第九章 存盤程序
初識存盤程序
存盤程序(Stored Procedure)是在大型資料庫系統中,一組為了完成特定功能的SQL 陳述句集,存盤在資料庫中,經過第一次編譯后呼叫不需要再次編譯,用戶通過指定存盤程序的名字并給出引數(如果該存盤程序帶有引數)來執行它,存盤程序是資料庫中的一個重要物件,
包含一系列PL/SQL陳述句的集合,
創建存盤格式
CREATE [OR REPLACE] PROCEDURE procedure_name
(argument1 [mode1] datatype1,
argument2 [mode2] datatype2, ...)
AS [IS]
宣告部分
BEGIN
執行部分
EXCEPTION
例外處理部分
END;
呼叫存盤程序格式
call proc_update_emp();
示例:
create or replace procedure pro_stu_update
as
begin
update student set stu_name='edit_name' where stu_id=5;
end;
-- 呼叫
call pro_stu_update();
in 示例
-- 根據員工號,查詢員工工資
create or replace procedure
-- in表示入參
pro_emp_selectArray(v_empId in employees.employee_id%type)
as v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id=v_empId;
DBMS_OUTPUT.PUT_LINE('salary:' || v_sal);
end;
-- call呼叫
call pro_emp_selectArray(198);
in/out 示例
-- 根據員工號,查詢員工工資 帶out引數
create or replace procedure
-- in表示入參,out表示出參
pro_emp_selectArray(v_empId in employees.employee_id%type,v_sal out employees.salary%type)
as
begin
select salary into v_sal from employees where employee_id=v_empId;
DBMS_OUTPUT.PUT_LINE('salary:' || v_sal);
end;
-- PL/SQL呼叫
declare
-- 對應的引數型別和數量保持一致
v_empId employees.employee_id%type := '&input_empId';
v_sal employees.salary%type;
begin
pro_emp_selectArray(v_empId,v_sal);
exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE('找不到對應員工');
end;
inout示例
create or replace procedure
-- in/out 引數共用,必須保持引數型別相同
pro_emp_selectArray(v_param in out number)
as
begin
select manager_id into v_param from employees where employee_id=v_param;
DBMS_OUTPUT.PUT_LINE('salary:' || v_param);
end;
-- PL/SQL呼叫
declare
v_param number := '&input_param';
begin
pro_emp_selectArray(v_param);
exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE('找不到對應員工');
end;
多引數傳遞 示例
create or replace procedure
pro_multi_params(param1 in number,param2 in number,param3 in number)
as v_sum number;
begin
v_sum := param1+param2+param3;
DBMS_OUTPUT.PUT_LINE('v_sum:' || v_sum);
end;
-- call呼叫
call pro_multi_params(1,2,3);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/11245.html
標籤:Oracle
