Oracle如何創建存儲過程和調用


oracle存儲過程的創建語法

create or replace procedure 存儲過程名稱
(
--定義輸入、輸出參數--
參數名1 in 參數類型,
參數名2 in 參數類型,
參數名3 in 參數類型,
參數名4 out 參數類型
)
as
--定義變量--
--變量名 變量數據類型;如:
 -- numCount integer; 
begin   
     --處理方法-
end;

上面我們創建一個處理加、減、乘、除計算的函數,那么我們也可以建成存儲過程

/*****
** 創建加、減、乘、除計算的存儲過程
**輸入參數: 數字1,數字2,計算類型
**輸出參數: 數字3
*****/
create or replace procedure Proc_Test
(
--定義輸入、輸出參數--
num_A in integer,
num_B in integer,
numType in integer,
num_C out integer
)
as
--定義變量--
 -- numCount integer;
 -- numStr varchar(20);  
begin   
     --判斷計算類型--
     if numType=1 then
        num_C := num_A + num_B;
     elsif numType=2 then
        num_C := num_A - num_B;
     elsif numType=3 then
        num_C := num_A * num_B; 
     elsif numType=4 then
        num_C := num_A / num_B; 
     else
     --其它處理
       dbms_output.put_line('其它處理');
     end if;
end;

那么如何調用存儲過程

declare num_C integer;
begin
   --調用存儲過程---
   Proc_Test(3,4,3,num_C);
   dbms_output.put_line('輸出結果:'|| num_C );
end;

輸出結果

img[/filehub/2021/01/14/ae0638c0-5b7d-4905-85f4-c5e1c1d80cee.png]

那么,如果我們要在存儲過程調用函數,是怎么處理的呢?

create or replace procedure Proc_Test
(
--定義輸入、輸出參數--
num_A in integer,
num_B in integer,
numType in integer,
num_C out integer
)
as
--定義變量--
 -- numStr varchar(20);  
begin   
   /*  --判斷計算類型--
     if numType=1 then
        num_C := num_A + num_B;
     elsif numType=2 then
        num_C := num_A - num_B;
     elsif numType=3 then
        num_C := num_A * num_B; 
     elsif numType=4 then
        num_C := num_A / num_B; 
     else
     --其它處理
       dbms_output.put_line('其它處理');
     end if;*/
     
     --調用函數 並賦值到num_C
     num_C:=fun_Test(num_A,num_B,numType);
end;

只要把處理過程放到函數中,存儲過程調用函數就可以了

再次調用的結果

declare num_C integer;
begin
   --調用存儲過程---
   Proc_Test(12,2,4,num_C);
   dbms_output.put_line('輸出結果:'|| num_C );
end;

輸出結果


免責聲明!

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



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