1. 程式人生 > >oracle對象之存儲函數

oracle對象之存儲函數

gin line clas 指定 函數名 實現 pre num ron

存儲函數

create or replace function 函數名(Name in type, Name out type, ...) return 數據類型 is

結果變量 數據類型;

begin

return(結果變量);

end[函數名];

存儲過程和存儲函數的區別

一般來講,過程和函數的區別在於函數可以有一個返回值;而過程沒有返回值。

但過程和函數都可以通過out指定一個或多個輸出參數。我們可以利用out參數,在過程和函數中實現返回多個值。 一般來講,有一個返回值的時候用存儲函數,多個返回值用存儲過程。 範例:使用存儲函數來查詢指定員工的年薪
 1 create or replace function empincome(eno in
emp.empno%type) return number is 2 3 psal emp.sal%type; 4 5 pcomm emp.comm%type; 6 7 begin 8 9 select t.sal into psal from emp t where t.empno = eno; 10 11 return psal * 12 + nvl(pcomm, 0); 12 13 end;

使用存儲過程來替換上面的例子
 1  
 2 create or replace procedure empincomep(eno in
emp.empno%type, income out number) is 3 4 psal emp.sal%type; 5 6 pcomm emp.comm%type; 7 8 begin 9 10 select t.sal, t.comm into psal, pcomm from emp t where t.empno = eno; 11 12 income := psal*12+nvl(pcomm,0); 13 14 end empincomep;

調用:
 1  
 2 declare
 3  
 4   income number;
5 6 begin 7 8 empincomep(7369, income); 9 10 dbms_output.put_line(income); 11 12 end;

oracle對象之存儲函數