2. 存儲(chǔ)過程內(nèi)部塊
2.1 內(nèi)部塊
我們知道了存儲(chǔ)過程的結(jié)構(gòu),語(yǔ)句塊由begin開始,以end結(jié)束。這些塊是可以嵌套。在語(yǔ)句塊中可以嵌套任何以下的塊。
- 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.存儲(chǔ)過程的常用技巧
3.1 哪種集合?
我們?cè)谑褂么鎯?chǔ)過程的時(shí)候經(jīng)常需要處理記錄集,也就是多條數(shù)據(jù)記錄。分為單列多行和多列多行,這些類型都可以稱為集合類型。我們?cè)谶@里進(jìn)行比較這些集合類型,以便于在編程時(shí)做出正確的選擇。
索引表,也稱為pl/sql表,不能存儲(chǔ)于數(shù)據(jù)庫(kù)中,元素的個(gè)數(shù)沒有限制,下標(biāo)可以為負(fù)值。
- 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)表示存放元素的數(shù)據(jù)類型,binary_integer表示元素下標(biāo)的數(shù)據(jù)類型。
嵌套表,索引表沒有 index by子句就是嵌套表,它可以存放于數(shù)據(jù)中,元素個(gè)數(shù)無限,下標(biāo)從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 ;
僅是這樣聲明是不能使用的,必須對(duì)嵌套表進(jìn)行初始化,對(duì)嵌套表進(jìn)行初始化可以使用它的構(gòu)造函數(shù)
- v_class?:=t_nestTable( 'a' , 'b' , 'c' );??
v_class :=t_nestTable('a','b','c');
變長(zhǎng)數(shù)組,變長(zhǎng)數(shù)組與高級(jí)語(yǔ)言的數(shù)組類型非常相似,下標(biāo)以1開始,元素個(gè)數(shù)有限。
- type?t_array?is?varray?( 20 )?of?varchar2( 20 );??
type t_array is varray (20) of varchar2(20);
varray(20)就定義了變長(zhǎng)數(shù)組的最大元素個(gè)數(shù)是20個(gè)
變長(zhǎng)數(shù)組與嵌套表一樣,也可以是數(shù)據(jù)表列的數(shù)據(jù)類型。
同時(shí),變長(zhǎng)數(shù)組的使用也需要事先初始化。
類型 可存儲(chǔ)于數(shù)據(jù)庫(kù) 元素個(gè)數(shù) 是否需初始化 初始下標(biāo)值
索引表 否 無限 不需
嵌套表 可 無限 需 1
可變數(shù)組 可 有限(自定義) 需 1
由此可見,如果僅僅是在存儲(chǔ)過程中當(dāng)作集合變量使用,索引表是最好的選擇。
3.2 選用何種游標(biāo)?
顯示游標(biāo)分為:普通游標(biāo),參數(shù)化游標(biāo)和游標(biāo)變量三種。
下面以一個(gè)過程來進(jìn)行說明
- 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
這一句是定義了一個(gè)最普通的游標(biāo),把整個(gè)查詢已經(jīng)寫死,調(diào)用時(shí)不可以作任何改變。
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
這一句并沒有寫死,查詢參數(shù)由變量v_rownum來決定。需要注意的是v_rownum必須在這個(gè)游標(biāo)定義之前聲明。
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
這一條語(yǔ)句與第二條作用相似,都是可以為游標(biāo)實(shí)現(xiàn)動(dòng)態(tài)的查詢。但是它進(jìn)一步的縮小了參數(shù)的作用域范圍。但是可讀性降低了不少。
type t_postype is ref cursor ;
c_postype3 t_postype;
先定義了一個(gè)引用游標(biāo)類型,然后再聲明了一個(gè)游標(biāo)變量。
open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
然后再用open for 來打開一個(gè)查詢。需要注意的是它可以多次使用,用來打開不同的查詢。
從動(dòng)態(tài)性來說,游標(biāo)變量是最好用的,但是閱讀性也是最差的。
注意,游標(biāo)的定義只能用使關(guān)鍵字IS,它與AS不通用。
3.3 游標(biāo)循環(huán)最佳策略
我們?cè)谶M(jìn)行PL/SQL編程時(shí),經(jīng)常需要循環(huán)讀取結(jié)果集的數(shù)據(jù)。進(jìn)行逐行處理,這個(gè)過程就需要對(duì)游標(biāo)進(jìn)行循環(huán)。對(duì)游標(biāo)進(jìn)行循環(huán)的方法有多種,我們?cè)诖艘灰环治觥?
- 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;
使用游標(biāo)之前需要開打游標(biāo),open cursor,循環(huán)完后再關(guān)閉游標(biāo)close cursor.
這是使用游標(biāo)應(yīng)該慎記于心的法則。
上面的過程演示了游標(biāo)循環(huán)的三種方法。
在討論循環(huán)方法之前,我們先看看%found和%notfound這些游標(biāo)的屬性。
- 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;
在打開一個(gè)游標(biāo)之后,馬上檢查它的%found或%notfound屬性,它得到的結(jié)果即不是true也不是false.而是null.必須執(zhí)行一條fetch語(yǔ)句后,這些屬性才有值。
第一種使用loop 循環(huán)
- 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語(yǔ)句一定要緊跟在fetch之后。必避免多余的數(shù)據(jù)處理。
處理邏輯需要跟在exit when之后。這一點(diǎn)需要多加小心。
循環(huán)結(jié)束后要記得關(guān)閉游標(biāo)。
第二種使用while循環(huán)。
- ???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;
我們知道了一個(gè)游標(biāo)打開后,必須執(zhí)行一次fetch語(yǔ)句,游標(biāo)的屬性才會(huì)起作用。所以使用while 循環(huán)時(shí),就需要在循環(huán)之前進(jìn)行一次fetch動(dòng)作。
而且數(shù)據(jù)處理動(dòng)作必須放在循環(huán)體內(nèi)的fetch方法之前。循環(huán)體內(nèi)的fetch方法要放在最后。否則就會(huì)多處理一次。這一點(diǎn)也要非常的小心。
總之,使用while來循環(huán)處理游標(biāo)是最復(fù)雜的方法。
第三種 for循環(huán)
- 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循環(huán)是比較簡(jiǎn)單實(shí)用的方法。
首先,它會(huì)自動(dòng)open和close游標(biāo)。解決了你忘記打開或關(guān)閉游標(biāo)的煩惱。
其它,自動(dòng)定義了一個(gè)記錄類型及聲明該類型的變量,并自動(dòng)fetch數(shù)據(jù)到這個(gè)變量中。
我們需要注意v_pos 這個(gè)變量無需要在循環(huán)外進(jìn)行聲明,無需要為其指定數(shù)據(jù)類型。
它應(yīng)該是一個(gè)記錄類型,具體的結(jié)構(gòu)是由游標(biāo)決定的。
這個(gè)變量的作用域僅僅是在循環(huán)體內(nèi)。
把v_pos看作一個(gè)記錄變量就可以了,如果要獲得某一個(gè)值就像調(diào)用記錄一樣就可以了。
如v_pos.pos_type
由此可見,for循環(huán)是用來循環(huán)游標(biāo)的最好方法。高效,簡(jiǎn)潔,安全。
但遺憾的是,常常見到的卻是第一種方法。所以從今之后得改變這個(gè)習(xí)慣了。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

