Oracle 存儲過程以及存儲函數


以下的一些例子是基於scott用戶下的emp表的數據,一和二使用的均為in,out參數,最后一個綜合練習使用了 in out參數

一.存儲過程

1.創建無參的存儲過程示例  ------ hello

注意存儲過程中的PLSQL部分沒有了declare

1 create or replace procedure hello 2 as
3 begin
4         dbms_output.put_line('ok'); 5 end; 6 /

2.如何調用?

第一種方式 -----  exec(或者是execute) hello;

第二種方式 ----- 使用PLSQL調用

    begin

    hello;
  end;
  /

3.創建有參存儲過程raiseSalary(編號),為指定員工漲10%的工資(員工編號從鍵盤接收)(in的用法)

注意:1.涉及到10%這種帶有百分號的運算時一定要轉換成相應的數字,例如:0.1

        2.in 表示是何種參數,為默認值,可省略

   3.&符號的使用,輸入時如果是字符串不要忘記' ',輸完不要多加分號

 1 --創建
 2 create or replace procedure raiseSalary(pempno in number)  3 as
 4 begin
 5          update emp set sal = sal * 1.1 where empno = pempno;  6 end;  7 /
 8 --調用(exec raiseSalary(&empno);)
 9 begin
10         raiseSalary(&empno); 11 end

4.創建有參存儲過程findEmpNameAndSalAndJob(編號),查詢指定編號員工的的姓名,職位,月薪,返回多個值(out的用法)

注意:用 || 連接字符串,以及使用 ' '表示字符串

 1 --創建
 2 create or replace procedure findEmpNameAndSalAndJob(pempno emp.empno%type,pname out emp.ename%type,pjob out emp.job%type,psal out emp.sal%type)  3 as
 4 begin
 5         select ename,job,sal into pname,pjob,psal from emp where empno = pempno;  6 end;  7 /
 8 --調用
 9 declare
10         ---創建這三個變量去接收傳出來的參數
11         pname emp.ename%type; 12         pjob emp.job%type; 13         psal emp.sal%type; 14 begin
15         findEmpNameAndSalAndJob(&empno,pname,pjob,psal); 16         dbms_output.put_line(pname || '----' || pjob || '-----' || psal ); 17 end; 18 /

總結:從上面的幾個例子可以看出如果有存儲過程有參數傳出的話,那么使用PLSQL調用較好,如果無傳出參數的話用exec調用

 

二.存儲函數

  存儲函數必須有且只有一個返回值和參數類型

1.創建並調用無參的存儲過程示例

注意:1.可以看到創建存儲函數時的PLSQL部分仍然沒有declare

   2.調用存儲函數時由於函數有返回值所以聲明了一個變量去接收

 1 --創建
 2 create or replace function getName return varchar2
 3 as
 4 begin
 5         return 'wyc';  6 end;  7 /
 8 
 9 --調用
10 declare 
11         name varchar2(10); 12 begin
13         name := getName(); 14  dbms_output.put_line(name); 15 end; 16 /

 

 

2.創建有參存儲函數findEmpIncome(編號),查詢指定員工編的年收入(年薪+佣金)(in的用法,默認是in)

 

 1 --創建
 2 create or replace function findEmpIncome(pempno in number) return number
 3 as
 4         income emp.sal%type;  5 begin
 6         -- 考慮到emp表中一些員工的佣金為null
 7         select sal + NVL(comm,0) into income from emp where empno = pempno;  8         return income;  9 end; 10 /
11 --調用
12 declare 
13         income emp.sal%type; 14 begin
15         income := findEmpIncome(&empno); 16         dbms_output.put_line('收入是' || income); 17 end; 18 /

 

 3.創建有參存儲函數findEmpNameAndJobAndSal(編號),查詢指定編號員工的的姓名(return),職位(out),月薪(out),返回多個值

  前面說過存儲函數只能有一個返回值,如果有多個返回值的話要結合out參數進行處理

 

 1 --創建函數
 2 create or replace function findEmpNameAndJobAndSal(pempno in number,pjob out varchar2,psal out varchar2)  3 return varchar2
 4 as
 5      pname emp.ename%type;  6 begin
 7         select ename,job,sal into pname,pjob,psal from emp where empno = pempno;  8         return pname;  9 end; 10 /
11 --調用函數
12 declare
13         pjob emp.job%type; 14         psal emp.sal%type; 15         pname emp.ename%type; --- 接收返回值
16 begin
17         pname :=findEmpNameAndJobAndSal(&empno,pjob,psal); 18         dbms_output.put_line(pname || '-----' || pjob || '-----' || psal); 19 end; 20 /

 總結:存儲函數與存儲過程有些類似但又有不同,只有一個返回值時適合使用存儲函數,有多個返回值或者沒有返回值時適合使用存儲過程

 

3.綜合練習  --- 分別使用存儲過程及存儲函數計算個人稅收(存儲函數使用到了in out參數)

 

 

1.存儲過程計算個人所得稅

 1 --創建存儲過程 (注意10%一定要寫成0.1)
 2 create or replace procedure get_rax(psal in number,rax out number)  3 as
 4         money number(6);  5 begin
 6         money := psal - 3500;  7         if money <= 1500 then 
 8             rax := money * 0.03 - 0;  9         elsif money <= 4500 then
10             rax := money * 0.1 - 105; 11         elsif money <= 9000 then
12             rax := money * 0.2 - 555; 13         elsif money <= 35000 then
14           rax := money * 0.25 - 1005; 15         elsif money <= 55000 then
16             rax := money * 0.3 - 2755; 17         elsif money <= 80000 then
18             rax := money * 0.35 - 5505; 19         else
20             rax := money * 0.45 - 13505; 21         end if; 22 end; 23 /
24 --調用
25 declare
26         rax number(20); 27 begin
28             get_rax(&sal,rax); 29             dbms_output.put_line('您要交的稅為 : ' || rax); 30 end; 31 /

 

 

2.存儲函數

pname要回傳給調用者,從而輸出姓名

 1 create or replace function get_rax2(pname in out varchar2,psal in number)  2 return number
 3 as
 4         money number(6);  5         rax number(20);  6 begin
 7         money := psal - 3500;  8         if money <= 1500 then 
 9             rax := money * 0.03 - 0; 10         elsif money <= 4500 then
11             rax := money * 0.1 - 105; 12         elsif money <= 9000 then
13             rax := money * 0.2 - 555; 14         elsif money <= 35000 then
15           rax := money * 0.25 - 1005; 16         elsif money <= 55000 then
17             rax := money * 0.3 - 2755; 18         elsif money <= 80000 then
19             rax := money * 0.35 - 5505; 20         else
21             rax := money * 0.45 - 13505; 22         end if; 23         return rax; 24 end; 25 /
26 
27 --調用存儲函數
28 declare
29         rax  number(20); 30         name varchar2(10); 31 begin
32          name := &name; 33          rax := get_rax2(name,&sal); 34          dbms_output.put_line('尊敬的' || name || ',您應交的稅為 :' || rax); 35 end; 36 /

 如果還有問題的話,后續再進行補充 ^_^

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM