2. 存儲過程內部塊
2.1 內部塊
我們知道了存儲過程的結構,語句塊由begin開始,以end結束。這些塊是可以嵌套。在語句塊中可以嵌套任何以下的塊。
- Declare?…?begin?…?exception?…?end; ??
- create?or?replace?procedure?innerBlock(p1?varchar2) ??
- as? ??
- ??o1?varchar2( 10 )?:=? 'out1' ; ??
- begin ??
- ??dbms_output.put_line(o1); ??
- ??declare? ??
- ????inner1?varchar2( 20 ); ??
- ??begin ??
- ????inner1?:= 'inner1' ; ??
- ????dbms_output.put_line(inner1); ??
- ??
- ????declare? ??
- ??????inner2?varchar2( 20 ); ??
- ????begin ??
- ??????inner2?:=? 'inner2' ; ??
- ??????dbms_output.put_line(inner2); ??
- ????end; ??
- ??exception? ??
- ????when?others?then ??
- ?????? null ; ??
- ??end; ??
- end;??
Declare … begin … exception … end; create or replace procedure innerBlock(p1 varchar2) as o1 varchar2(10) := 'out1'; begin dbms_output.put_line(o1); declare inner1 varchar2(20); begin inner1 :='inner1'; dbms_output.put_line(inner1); declare inner2 varchar2(20); begin inner2 := 'inner2'; dbms_output.put_line(inner2); end; exception when others then null; end; end;
需要注意變量的作用域。
3.存儲過程的常用技巧
3.1 哪種集合?
我們在使用存儲過程的時候經常需要處理記錄集,也就是多條數據記錄。分為單列多行和多列多行,這些類型都可以稱為集合類型。我們在這里進行比較這些集合類型,以便于在編程時做出正確的選擇。
索引表,也稱為pl/sql表,不能存儲于數據庫中,元素的個數沒有限制,下標可以為負值。
- type?t_table?is?table?of?varchar2( 20 )?index?by?binary_integer; ??
- ?v_student?t_table;??
type t_table is table of varchar2(20) index by binary_integer; v_student t_table;
varchar2(20)表示存放元素的數據類型,binary_integer表示元素下標的數據類型。
嵌套表,索引表沒有 index by子句就是嵌套表,它可以存放于數據中,元素個數無限,下標從1開始,并且需要初始化
- type?t_nestTable?is?table?of?varchar2( 20 ); ??
- v_class?t_nestTable?;??
type t_nestTable is table of varchar2(20); v_class t_nestTable ;
僅是這樣聲明是不能使用的,必須對嵌套表進行初始化,對嵌套表進行初始化可以使用它的構造函數
- v_class?:=t_nestTable( 'a' , 'b' , 'c' );??
v_class :=t_nestTable('a','b','c');
變長數組,變長數組與高級語言的數組類型非常相似,下標以1開始,元素個數有限。
- type?t_array?is?varray?( 20 )?of?varchar2( 20 );??
type t_array is varray (20) of varchar2(20);
varray(20)就定義了變長數組的最大元素個數是20個
變長數組與嵌套表一樣,也可以是數據表列的數據類型。
同時,變長數組的使用也需要事先初始化。
類型 可存儲于數據庫 元素個數 是否需初始化 初始下標值
索引表 否 無限 不需
嵌套表 可 無限 需 1
可變數組 可 有限(自定義) 需 1
由此可見,如果僅僅是在存儲過程中當作集合變量使用,索引表是最好的選擇。
3.2 選用何種游標?
顯示游標分為:普通游標,參數化游標和游標變量三種。
下面以一個過程來進行說明
- create?or?replace?procedure?proccursor(p?varchar2) ??
- as? ??
- v_rownum?number( 10 )?:=? 1 ; ??
- cursor?c_postype?is?select?pos_type?from?pos_type_tbl?where?rownum?= 1 ; ??
- cursor?c_postype1?is?select?pos_type?from?pos_type_tbl?where?rownum?=?v_rownum; ??
- cursor?c_postype2(p_rownum?number)?is?select?pos_type?from?pos_type_tbl?where?rownum?=?p_rownum; ??
- type?t_postype?is?ref?cursor?; ??
- c_postype3?t_postype; ??
- v_postype?varchar2( 20 ); ??
- begin ??
- ??open?c_postype; ??
- ??fetch?c_postype?into?v_postype; ??
- ??dbms_output.put_line(v_postype); ??
- ??close?c_postype; ??
- ??open?c_postype1; ??
- ??fetch?c_postype1?into?v_postype; ??
- ??dbms_output.put_line(v_postype); ??
- ??close?c_postype1; ??
- ??open?c_postype2( 1 ); ??
- ??fetch?c_postype2?into?v_postype; ??
- ??dbms_output.put_line(v_postype); ??
- ??close?c_postype2; ??
- ??open?c_postype3? for ?select?pos_type?from?pos_type_tbl?where?rownum?= 1 ; ??
- ??fetch?c_postype3?into?v_postype; ??
- ??dbms_output.put_line(v_postype); ??
- ??close?c_postype3; ??
- end;??
create or replace procedure proccursor(p varchar2) as v_rownum number(10) := 1; cursor c_postype is select pos_type from pos_type_tbl where rownum =1; cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum; cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum; type t_postype is ref cursor ; c_postype3 t_postype; v_postype varchar2(20); begin open c_postype; fetch c_postype into v_postype; dbms_output.put_line(v_postype); close c_postype; open c_postype1; fetch c_postype1 into v_postype; dbms_output.put_line(v_postype); close c_postype1; open c_postype2(1); fetch c_postype2 into v_postype; dbms_output.put_line(v_postype); close c_postype2; open c_postype3 for select pos_type from pos_type_tbl where rownum =1; fetch c_postype3 into v_postype; dbms_output.put_line(v_postype); close c_postype3; end;
cursor c_postype is select pos_type from pos_type_tbl where rownum =1
這一句是定義了一個最普通的游標,把整個查詢已經寫死,調用時不可以作任何改變。
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
這一句并沒有寫死,查詢參數由變量v_rownum來決定。需要注意的是v_rownum必須在這個游標定義之前聲明。
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
這一條語句與第二條作用相似,都是可以為游標實現動態的查詢。但是它進一步的縮小了參數的作用域范圍。但是可讀性降低了不少。
type t_postype is ref cursor ;
c_postype3 t_postype;
先定義了一個引用游標類型,然后再聲明了一個游標變量。
open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
然后再用open for 來打開一個查詢。需要注意的是它可以多次使用,用來打開不同的查詢。
從動態性來說,游標變量是最好用的,但是閱讀性也是最差的。
注意,游標的定義只能用使關鍵字IS,它與AS不通用。
3.3 游標循環最佳策略
我們在進行PL/SQL編程時,經常需要循環讀取結果集的數據。進行逐行處理,這個過程就需要對游標進行循環。對游標進行循環的方法有多種,我們在此一一分析。
- create?or?replace?procedure?proccycle(p?varchar2) ??
- as? ??
- cursor?c_postype?is?select?pos_type,?description?from?pos_type_tbl?where?rownum?<? 6 ; ??
- v_postype?varchar2( 20 ); ??
- v_description?varchar2( 50 ); ??
- begin ??
- open?c_postype; ??
- ?? if ?c_postype%found?then ??
- ????dbms_output.put_line( 'found?true' ); ??
- ??elsif?c_postype%found?=? false ?then ??
- ????dbms_output.put_line( 'found?false' ); ??
- ?? else ??
- ????dbms_output.put_line( 'found?null' ); ??
- ??end? if ; ??
- ??loop ??
- ???fetch?c_postype?into?v_postype,v_description?; ??
- ???exit?when?c_postype%notfound; ??
- ???dbms_output.put_line( 'postype:' ||v_postype|| ',description:' ||v_description); ??
- ??end?loop; ??
- ??close?c_postype; ??
- dbms_output.put_line( '---loop?end---' ); ??
- ??open?c_postype; ??
- ????fetch?c_postype?into?v_postype,v_description; ??
- ???? while ?c_postype%found?loop ??
- ??????dbms_output.put_line( 'postype:' ||v_postype|| ',description:' ||v_description); ??
- ??????fetch?c_postype?into?v_postype,v_description?; ??
- ????end?loop; ??
- ??
- ??close?c_postype; ??
- dbms_output.put_line( '---while?end---' ); ??
- ?? for ?v_pos?in?c_postype?loop ??
- ????v_postype?:=?v_pos.pos_type; ??
- ????v_description?:=?v_pos.description; ??
- ????dbms_output.put_line( 'postype:' ||v_postype|| ',description:' ||v_description); ??
- ??end?loop; ??
- ??dbms_output.put_line( '---for?end---' ); ??
- end;??
create or replace procedure proccycle(p varchar2) as cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6; v_postype varchar2(20); v_description varchar2(50); begin open c_postype; if c_postype%found then dbms_output.put_line('found true'); elsif c_postype%found = false then dbms_output.put_line('found false'); else dbms_output.put_line('found null'); end if; loop fetch c_postype into v_postype,v_description ; exit when c_postype%notfound; dbms_output.put_line('postype:'||v_postype||',description:'||v_description); end loop; close c_postype; dbms_output.put_line('---loop end---'); open c_postype; fetch c_postype into v_postype,v_description; while c_postype%found loop dbms_output.put_line('postype:'||v_postype||',description:'||v_description); fetch c_postype into v_postype,v_description ; end loop; close c_postype; dbms_output.put_line('---while end---'); for v_pos in c_postype loop v_postype := v_pos.pos_type; v_description := v_pos.description; dbms_output.put_line('postype:'||v_postype||',description:'||v_description); end loop; dbms_output.put_line('---for end---'); end;
使用游標之前需要開打游標,open cursor,循環完后再關閉游標close cursor.
這是使用游標應該慎記于心的法則。
上面的過程演示了游標循環的三種方法。
在討論循環方法之前,我們先看看%found和%notfound這些游標的屬性。
- open?c_postype; ??
- ? if ?c_postype%found?then ??
- ???dbms_output.put_line( 'found?true' ); ??
- ?elsif?c_postype%found?=? false ?then ??
- ???dbms_output.put_line( 'found?false' ); ??
- ? else ??
- ???dbms_output.put_line( 'found?null' ); ??
- ?end? if ;??
open c_postype; if c_postype%found then dbms_output.put_line('found true'); elsif c_postype%found = false then dbms_output.put_line('found false'); else dbms_output.put_line('found null'); end if;
在打開一個游標之后,馬上檢查它的%found或%notfound屬性,它得到的結果即不是true也不是false.而是null.必須執行一條fetch語句后,這些屬性才有值。
第一種使用loop 循環
- loop ??
- ???fetch?c_postype?into?v_postype,v_description?; ??
- ???exit?when?c_postype%notfound; ??
- ???…… ??
- end?loop??
loop fetch c_postype into v_postype,v_description ; exit when c_postype%notfound; …… end loop
這里需要注意,exit when語句一定要緊跟在fetch之后。必避免多余的數據處理。
處理邏輯需要跟在exit when之后。這一點需要多加小心。
循環結束后要記得關閉游標。
第二種使用while循環。
- ???fetch?c_postype?into?v_postype,v_description; ??
- while ?c_postype%found?loop ??
- ???…… ??
- ??????fetch?c_postype?into?v_postype,v_description?; ??
- end?loop;??
fetch c_postype into v_postype,v_description; while c_postype%found loop …… fetch c_postype into v_postype,v_description ; end loop;
我們知道了一個游標打開后,必須執行一次fetch語句,游標的屬性才會起作用。所以使用while 循環時,就需要在循環之前進行一次fetch動作。
而且數據處理動作必須放在循環體內的fetch方法之前。循環體內的fetch方法要放在最后。否則就會多處理一次。這一點也要非常的小心。
總之,使用while來循環處理游標是最復雜的方法。
第三種 for循環
- for ?v_pos?in?c_postype?loop ??
- ???v_postype?:=?v_pos.pos_type; ??
- ???v_description?:=?v_pos.description; ??
- ???… ??
- ?end?loop;??
for v_pos in c_postype loop v_postype := v_pos.pos_type; v_description := v_pos.description; … end loop;
可見for循環是比較簡單實用的方法。
首先,它會自動open和close游標。解決了你忘記打開或關閉游標的煩惱。
其它,自動定義了一個記錄類型及聲明該類型的變量,并自動fetch數據到這個變量中。
我們需要注意v_pos 這個變量無需要在循環外進行聲明,無需要為其指定數據類型。
它應該是一個記錄類型,具體的結構是由游標決定的。
這個變量的作用域僅僅是在循環體內。
把v_pos看作一個記錄變量就可以了,如果要獲得某一個值就像調用記錄一樣就可以了。
如v_pos.pos_type
由此可見,for循環是用來循環游標的最好方法。高效,簡潔,安全。
但遺憾的是,常常見到的卻是第一種方法。所以從今之后得改變這個習慣了。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
