天天看點

oracle的存儲過程及其基本文法

Oracle存儲過程詳解:https://www.2cto.com/database/201802/723493.html(比較好)

oracle中的存儲過程例子:https://www.cnblogs.com/zhao123/p/3911537.html

Oracle存儲過程入門及基礎文法:https://www.cnblogs.com/lideng/p/3427822.html

基本文法介紹

可以看出,基本的功能實作,調用完成。

下面,來看看基本文法:

1,變量指派

變量名 := 值;

2,判斷語句。

if 比較式 then

begin

            ......

        end;

end
           

if

結合起來寫個簡單例子:

注意事項:

存儲過程參數不帶取值範圍,in表示傳入,out表示輸出; 變量帶取值範圍,後面接分号; 在判斷語句前最好先用count(*)函數判斷是否存在該條操作記錄; 用select … into … 給變量指派; 在代碼中抛異常用 raise+異常名;

存儲過程建立的文法:

create [or replace] procedure 存儲過程名(param1 in type,param2 out type)
as
變量1 類型(值範圍);
變量2 類型(值範圍);
Begin
    Select count(*) into 變量1 from 表A where列名=param1;
 
    If (判斷條件) then
       Select 列名 into 變量2 from 表A where列名=param1;
       Dbms_output.Put_line(‘列印資訊’);
    Elsif (判斷條件) then
       Dbms_output.Put_line(‘列印資訊’);
    Else
       Raise 異常名(NO_DATA_FOUND);
    End if;
Exception
    When others then
       Rollback;
End;
           

例子:

create or replace procedure Test(x in out number)
is
begin
     if x<0 then
         begin
             x:= 0 - x;
        end;
     elsif x > 0 then     --noted here elsif 
         begin
             x:= x ;
        end;
     else
        x:=    0;
     end if;
end Test;
Test:
set serveroutput on;  --沒這句話,看不到dmbs_output資訊。
declare
       num number;
begin
    num:= -1;
    test(num);
    dbms_output.put_line( 'num = ' || num );
end;
/******************************
num = 1
PL/SQL procedure successfully completed.
*******************************/