Oracle 基礎 ----procedure(存儲過程)
存儲過程是一種命名pl/sql程序塊,它可以被賦予參數,存儲在數据庫中,可以被用戶調用,由於存儲過程是已經編譯好的代碼,所以在調用的時候不必再進行編譯,從而提高程序的運行效率。
同時,使用存儲過程可以實現程序的模塊化設計。
例子:
Create procedure insertmer as
Begin
Inser into mar values(‘’);
Exception
When dup_val_on_index then
Dbms_output.put_line(‘duplicate’);
When others then
Dbms_output.put_line(‘others’);
End insertmer;
存儲過程調用:
Ser serveroutput on
Begin
Insertmer;
End;
或者直接執行:
Execute Insertmer;
修改存儲過程使用:
Create or replace
參數:
In,out, in out.
該類型的參數值由調用者傳入,並且只能夠存儲過程讀取。
例子:
Create or replace procedure insermer(
P_id in varchar2,
P_name in varchar2,
P_price in number,
P_place in varchar2) as
Begin
Null;
Exception
Null;
end
在帶參數的變量中,不能固定參數的長度
在下面這個過程中,通過out參數返回值,表示在調用它時必須提供能夠接收返回值的變量。
SYS AS SYSDBA on 2008-03-01 10:28:39 at ORCL>edit
已將 file afiedt.buf 寫入
1 create or replace procedure searchpro(
2 id in number,
3 o_name out number,
4 o_price out number) is
5 begin
6 o_name:=id;
7 o_price:=id+1;
8 exception
9 when others then
10 dbms_output.put_line('o_name:'||o_name||' oprice:'||'oprice');
11* end searchpro;
SYS AS SYSDBA on 2008-03-01 10:28:55 at ORCL>/
已建立程序.
SYS AS SYSDBA on 2008-03-01 10:28:58 at ORCL>variable name number;
SYS AS SYSDBA on 2008-03-01 10:29:10 at ORCL>variable price number;
SYS AS SYSDBA on 2008-03-01 10:29:17 at ORCL>exec searchpro(1,name,price);
BEGIN searchpro(1,name,price); END;
*
ERROR 在行 1:
ORA-06550: line 1, column 19:
PLS-00201: identifier 'NAME' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SYS AS SYSDBA on 2008-03-01 10:29:32 at ORCL>exec searchpro(1,:name,:price);
PL/SQL 程序順利完成.
SYS AS SYSDBA on 2008-03-01 10:30:03 at ORCL>print name;
NAME
----------
1
SYS AS SYSDBA on 2008-03-01 10:30:11 at ORCL>print price
PRICE
----------
2
SYS AS SYSDBA on 2008-03-01 10:30:15 at ORCL>create or replace procedure swap(p_num1 in out nu
mber,p_num2 in out number) is
2 var_temp number;
3 begin
4 var_temp:=p_num1;
5 p_num1:=p_num2;
6 p_num2:=var_temp;
7 end swap;
8 /
已建立程序.
SYS AS SYSDBA on 2008-03-01 10:41:30 at ORCL>set serveroutput on
SYS AS SYSDBA on 2008-03-01 10:41:37 at ORCL>declare
2 var_max number:=20;
3 var_min number:=28;
4 begin
5 if var_max
6 swap(var_max,var_min);
7 end if;
8 dbms_output.put_line('var_max='||var_max);
9 dbms_output.put_line('var_min='||var_min);
10 end;
11 /
var_max=28
var_min=20
在使用in out 參數時,必須為in out 參數提供變量,不可以提供常量,因爲out .
在使用int 參數時候,可以使用default 默認值,而在 out,in out 參數時,不能使用default 默認值。
局部變量 和 子過程
同匿名pl/sql 程序塊一樣,過程也可以定義局部變量
SYS AS SYSDBA on 2008-03-01 10:44:55 at ORCL>create or replace procedure updatepro(p_Id in num
ber,p_rebate in number) is
2 var_rebate number;
3 procedure checkvalue(rebate in out number) is
4 begin
5 if rebate=null then
6 rebate:=1;
7 elsif rebate<0 then
8 rebate:=0.1;
9 elsif rebate>1 then
10 rebate:=1;
11 end if;
12 end checkvalue;
13 begin
14 var_rebate:=p_rebate;
15 checkvalue(var_rebate);
16 dbms_output.put_line('var_rebate:'||var_rebate);
17 end updatepro;
18 /
已建立程序.
SYS AS SYSDBA on 2008-03-01 10:56:52 at ORCL>set serveroutput on
SYS AS SYSDBA on 2008-03-01 10:57:37 at ORCL>begin
2 updatepro(1,2);
3 end;
4 /
var_rebate:1
PL/SQL 程序順利完成.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/701141/viewspace-198165/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/701141/viewspace-198165/