ORACLE 儲存過程

2022-08-19 09:51:06 字數 2047 閱讀 2557

前奏:

1:必要的概念:

oracle 提供能夠把 pl/sql 程式儲存在資料庫中。並能夠在不論什麼地方來執行它。這樣就叫儲存過

程或函式。

過程和函式統稱為 pl/sql 子程式,他們是被命名的 pl/sql 塊,均儲存在資料庫中,並

通過輸入、輸出引數或輸入/輸出引數與其呼叫者交換資訊。

過程和函式的唯一差別是函式總向調

用者返回資料,而過程則不返回資料。

2:建立儲存過程

在 oracle server 上建立儲存過程,能夠被多個應用程式呼叫,能夠向儲存過程傳遞引數,也能夠向儲存

過程傳回引數

建立過程語法:

create [or replace] procedure procedure_name

[ (argment [ ] type,

argment [ ] type ]

[ authid definer | current_user ]

《型別.變數的說明》

begin

《執行部分》

exception

《可選的異常錯誤處理程式》

end;

3:例題:

--定義乙個儲存過程,獲取給定部門的工資總和(通過out引數)。要求部門號和工資總和定義為引數

create or replace procedure get_sal3(dept_id number , sum_salary out number)

iscursor salary_cursor is select salary

from employees where department_id = dept_id;

begin

sum_salary := 0;

for c in salary_cursor loop

sum_salary := sum_salary + c.salary;

end loop;

dbms_output.put_line(sum_salary);

end;

select get_sal3(60,)

--對給定部門的員工進行加薪操作。若其到公司的時間是在(?-95)期間,為其加薪5%

--(95,98)加薪3%,(98,如今)加薪1%,得到下面返回結果:

--此次加薪公司每乙個月額外付出多少成本(定義乙個out型的輸出引數)

create or replace procedure get_money(dept_id number , temp_sal out number)

isv_i number(4,2):=0;

cursor salary_cursor is select employee_id , salary ,hire_date

from employees where department_id = dept_id;

begin

temp_sal := 0;

for c in salary_cursor loop

if to_char(c.hire_date,'yyyy')<'1995' then v_i := 0.05;

elsif to_char(c.hire_date,'yyyy')<'1998' then v_i := 0.03;

else v_i := 0.01 ;

end if;

--更新工資

update employees set salary = salary * (1+v_i)

where employee_id = c.employee_id;

--計算付出的成本

temp_sal := temp_sal + c.salary*v_i;

end loop;

dbms_output.put_line(temp_sal);

end;

4:呼叫儲存過程:

declare

v_temp number(10):=0;

begin

get_money(80,v_temp);

end;

Oracle儲存過程呼叫儲存過程

oracle儲存過程呼叫有返回結果集的儲存過程一般用光標的方式,宣告乙個游標,把結果集放到游標裡面,然後迴圈游標 declare newcs sys refcursor cs1 number cs2 number cstype table rowtype table列的個數和newcs返回的個數一樣...

ORACLE儲存過程

自定義函式開始 create or replace function fn wftemplateidget templatecategoryid number,organid number,templatemode number return number istemplateid number i...

Oracle 儲存過程

create or replace procedure p 有就替換,沒有就建立 iscursor c is select from emp for update begin for v emp in c loop if v emp.deptno 10 then update emp2 set sa...