黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

存儲過程常用技巧2

系統(tǒng) 2112 0

2. 存儲過程內(nèi)部塊
2.1 內(nèi)部塊
我們知道了存儲過程的結(jié)構(gòu),語句塊由begin開始,以end結(jié)束。這些塊是可以嵌套。在語句塊中可以嵌套任何以下的塊。

Java代碼 復(fù)制代碼
  1. Declare?…?begin?…?exception?…?end; ??
  2. create?or?replace?procedure?innerBlock(p1?varchar2) ??
  3. as? ??
  4. ??o1?varchar2( 10 )?:=? 'out1' ; ??
  5. begin ??
  6. ??dbms_output.put_line(o1); ??
  7. ??declare? ??
  8. ????inner1?varchar2( 20 ); ??
  9. ??begin ??
  10. ????inner1?:= 'inner1' ; ??
  11. ????dbms_output.put_line(inner1); ??
  12. ??
  13. ????declare? ??
  14. ??????inner2?varchar2( 20 ); ??
  15. ????begin ??
  16. ??????inner2?:=? 'inner2' ; ??
  17. ??????dbms_output.put_line(inner2); ??
  18. ????end; ??
  19. ??exception? ??
  20. ????when?others?then ??
  21. ?????? null ; ??
  22. ??end; ??
  23. 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 哪種集合?
我們在使用存儲過程的時候經(jīng)常需要處理記錄集,也就是多條數(shù)據(jù)記錄。分為單列多行和多列多行,這些類型都可以稱為集合類型。我們在這里進行比較這些集合類型,以便于在編程時做出正確的選擇。
索引表,也稱為pl/sql表,不能存儲于數(shù)據(jù)庫中,元素的個數(shù)沒有限制,下標(biāo)可以為負(fù)值。

Java代碼 復(fù)制代碼
  1. type?t_table?is?table?of?varchar2( 20 )?index?by?binary_integer; ??
  2. ?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ù)中,元素個數(shù)無限,下標(biāo)從1開始,并且需要初始化

Java代碼 復(fù)制代碼
  1. type?t_nestTable?is?table?of?varchar2( 20 ); ??
  2. v_class?t_nestTable?;??
    type t_nestTable is table of varchar2(20);
v_class t_nestTable ;
  

僅是這樣聲明是不能使用的,必須對嵌套表進行初始化,對嵌套表進行初始化可以使用它的構(gòu)造函數(shù)

Java代碼 復(fù)制代碼
  1. v_class?:=t_nestTable( 'a' , 'b' , 'c' );??
    v_class :=t_nestTable('a','b','c');
  

變長數(shù)組,變長數(shù)組與高級語言的數(shù)組類型非常相似,下標(biāo)以1開始,元素個數(shù)有限。

Java代碼 復(fù)制代碼
  1. type?t_array?is?varray?( 20 )?of?varchar2( 20 );??
    type t_array is varray (20) of varchar2(20);
  


varray(20)就定義了變長數(shù)組的最大元素個數(shù)是20個
變長數(shù)組與嵌套表一樣,也可以是數(shù)據(jù)表列的數(shù)據(jù)類型。
同時,變長數(shù)組的使用也需要事先初始化。

類型 可存儲于數(shù)據(jù)庫 元素個數(shù) 是否需初始化 初始下標(biāo)值
索引表 否 無限 不需
嵌套表 可 無限 需 1
可變數(shù)組 可 有限(自定義) 需 1

由此可見,如果僅僅是在存儲過程中當(dāng)作集合變量使用,索引表是最好的選擇。

3.2 選用何種游標(biāo)?
顯示游標(biāo)分為:普通游標(biāo),參數(shù)化游標(biāo)和游標(biāo)變量三種。
下面以一個過程來進行說明

Java代碼 復(fù)制代碼
  1. create?or?replace?procedure?proccursor(p?varchar2) ??
  2. as? ??
  3. v_rownum?number( 10 )?:=? 1 ; ??
  4. cursor?c_postype?is?select?pos_type?from?pos_type_tbl?where?rownum?= 1 ; ??
  5. cursor?c_postype1?is?select?pos_type?from?pos_type_tbl?where?rownum?=?v_rownum; ??
  6. cursor?c_postype2(p_rownum?number)?is?select?pos_type?from?pos_type_tbl?where?rownum?=?p_rownum; ??
  7. type?t_postype?is?ref?cursor?; ??
  8. c_postype3?t_postype; ??
  9. v_postype?varchar2( 20 ); ??
  10. begin ??
  11. ??open?c_postype; ??
  12. ??fetch?c_postype?into?v_postype; ??
  13. ??dbms_output.put_line(v_postype); ??
  14. ??close?c_postype; ??
  15. ??open?c_postype1; ??
  16. ??fetch?c_postype1?into?v_postype; ??
  17. ??dbms_output.put_line(v_postype); ??
  18. ??close?c_postype1; ??
  19. ??open?c_postype2( 1 ); ??
  20. ??fetch?c_postype2?into?v_postype; ??
  21. ??dbms_output.put_line(v_postype); ??
  22. ??close?c_postype2; ??
  23. ??open?c_postype3? for ?select?pos_type?from?pos_type_tbl?where?rownum?= 1 ; ??
  24. ??fetch?c_postype3?into?v_postype; ??
  25. ??dbms_output.put_line(v_postype); ??
  26. ??close?c_postype3; ??
  27. 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
這一句是定義了一個最普通的游標(biāo),把整個查詢已經(jīng)寫死,調(diào)用時不可以作任何改變。
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
這一句并沒有寫死,查詢參數(shù)由變量v_rownum來決定。需要注意的是v_rownum必須在這個游標(biāo)定義之前聲明。
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
這一條語句與第二條作用相似,都是可以為游標(biāo)實現(xiàn)動態(tài)的查詢。但是它進一步的縮小了參數(shù)的作用域范圍。但是可讀性降低了不少。
type t_postype is ref cursor ;
c_postype3 t_postype;
先定義了一個引用游標(biāo)類型,然后再聲明了一個游標(biāo)變量。
open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
然后再用open for 來打開一個查詢。需要注意的是它可以多次使用,用來打開不同的查詢。
從動態(tài)性來說,游標(biāo)變量是最好用的,但是閱讀性也是最差的。
注意,游標(biāo)的定義只能用使關(guān)鍵字IS,它與AS不通用。

3.3 游標(biāo)循環(huán)最佳策略
我們在進行PL/SQL編程時,經(jīng)常需要循環(huán)讀取結(jié)果集的數(shù)據(jù)。進行逐行處理,這個過程就需要對游標(biāo)進行循環(huán)。對游標(biāo)進行循環(huán)的方法有多種,我們在此一一分析。

Java代碼 復(fù)制代碼
  1. create?or?replace?procedure?proccycle(p?varchar2) ??
  2. as? ??
  3. cursor?c_postype?is?select?pos_type,?description?from?pos_type_tbl?where?rownum?<? 6 ; ??
  4. v_postype?varchar2( 20 ); ??
  5. v_description?varchar2( 50 ); ??
  6. begin ??
  7. open?c_postype; ??
  8. ?? if ?c_postype%found?then ??
  9. ????dbms_output.put_line( 'found?true' ); ??
  10. ??elsif?c_postype%found?=? false ?then ??
  11. ????dbms_output.put_line( 'found?false' ); ??
  12. ?? else ??
  13. ????dbms_output.put_line( 'found?null' ); ??
  14. ??end? if ; ??
  15. ??loop ??
  16. ???fetch?c_postype?into?v_postype,v_description?; ??
  17. ???exit?when?c_postype%notfound; ??
  18. ???dbms_output.put_line( 'postype:' ||v_postype|| ',description:' ||v_description); ??
  19. ??end?loop; ??
  20. ??close?c_postype; ??
  21. dbms_output.put_line( '---loop?end---' ); ??
  22. ??open?c_postype; ??
  23. ????fetch?c_postype?into?v_postype,v_description; ??
  24. ???? while ?c_postype%found?loop ??
  25. ??????dbms_output.put_line( 'postype:' ||v_postype|| ',description:' ||v_description); ??
  26. ??????fetch?c_postype?into?v_postype,v_description?; ??
  27. ????end?loop; ??
  28. ??
  29. ??close?c_postype; ??
  30. dbms_output.put_line( '---while?end---' ); ??
  31. ?? for ?v_pos?in?c_postype?loop ??
  32. ????v_postype?:=?v_pos.pos_type; ??
  33. ????v_description?:=?v_pos.description; ??
  34. ????dbms_output.put_line( 'postype:' ||v_postype|| ',description:' ||v_description); ??
  35. ??end?loop; ??
  36. ??dbms_output.put_line( '---for?end---' ); ??
  37. 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)的屬性。

Java代碼 復(fù)制代碼
  1. open?c_postype; ??
  2. ? if ?c_postype%found?then ??
  3. ???dbms_output.put_line( 'found?true' ); ??
  4. ?elsif?c_postype%found?=? false ?then ??
  5. ???dbms_output.put_line( 'found?false' ); ??
  6. ? else ??
  7. ???dbms_output.put_line( 'found?null' ); ??
  8. ?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;
  

在打開一個游標(biāo)之后,馬上檢查它的%found或%notfound屬性,它得到的結(jié)果即不是true也不是false.而是null.必須執(zhí)行一條fetch語句后,這些屬性才有值。

第一種使用loop 循環(huán)

Java代碼 復(fù)制代碼
  1. loop ??
  2. ???fetch?c_postype?into?v_postype,v_description?; ??
  3. ???exit?when?c_postype%notfound; ??
  4. ???…… ??
  5. end?loop??
    loop
   fetch c_postype into v_postype,v_description ;
   exit when c_postype%notfound;
   ……
end loop
  

這里需要注意,exit when語句一定要緊跟在fetch之后。必避免多余的數(shù)據(jù)處理。
處理邏輯需要跟在exit when之后。這一點需要多加小心。
循環(huán)結(jié)束后要記得關(guān)閉游標(biāo)。

第二種使用while循環(huán)。

Java代碼 復(fù)制代碼
  1. ???fetch?c_postype?into?v_postype,v_description; ??
  2. while ?c_postype%found?loop ??
  3. ???…… ??
  4. ??????fetch?c_postype?into?v_postype,v_description?; ??
  5. 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;
  


我們知道了一個游標(biāo)打開后,必須執(zhí)行一次fetch語句,游標(biāo)的屬性才會起作用。所以使用while 循環(huán)時,就需要在循環(huán)之前進行一次fetch動作。
而且數(shù)據(jù)處理動作必須放在循環(huán)體內(nèi)的fetch方法之前。循環(huán)體內(nèi)的fetch方法要放在最后。否則就會多處理一次。這一點也要非常的小心。
總之,使用while來循環(huán)處理游標(biāo)是最復(fù)雜的方法。

第三種 for循環(huán)

Java代碼 復(fù)制代碼
  1. for ?v_pos?in?c_postype?loop ??
  2. ???v_postype?:=?v_pos.pos_type; ??
  3. ???v_description?:=?v_pos.description; ??
  4. ???… ??
  5. ?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)是比較簡單實用的方法。
首先,它會自動open和close游標(biāo)。解決了你忘記打開或關(guān)閉游標(biāo)的煩惱。
其它,自動定義了一個記錄類型及聲明該類型的變量,并自動fetch數(shù)據(jù)到這個變量中。
我們需要注意v_pos 這個變量無需要在循環(huán)外進行聲明,無需要為其指定數(shù)據(jù)類型。
它應(yīng)該是一個記錄類型,具體的結(jié)構(gòu)是由游標(biāo)決定的。
這個變量的作用域僅僅是在循環(huán)體內(nèi)。
把v_pos看作一個記錄變量就可以了,如果要獲得某一個值就像調(diào)用記錄一樣就可以了。
如v_pos.pos_type
由此可見,for循環(huán)是用來循環(huán)游標(biāo)的最好方法。高效,簡潔,安全。
但遺憾的是,常常見到的卻是第一種方法。所以從今之后得改變這個習(xí)慣了。

存儲過程常用技巧2


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論