標籤:例子 預存程序 tput 查詢 對象 返回 str number output
儲存函數
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對象之儲存函數