亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

存儲過程常用技巧2

系統 1687 0

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

Java代碼 復制代碼
  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 哪種集合?
我們在使用存儲過程的時候經常需要處理記錄集,也就是多條數據記錄。分為單列多行和多列多行,這些類型都可以稱為集合類型。我們在這里進行比較這些集合類型,以便于在編程時做出正確的選擇。
索引表,也稱為pl/sql表,不能存儲于數據庫中,元素的個數沒有限制,下標可以為負值。

Java代碼 復制代碼
  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)表示存放元素的數據類型,binary_integer表示元素下標的數據類型。
嵌套表,索引表沒有 index by子句就是嵌套表,它可以存放于數據中,元素個數無限,下標從1開始,并且需要初始化

Java代碼 復制代碼
  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 ;
  

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

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

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

Java代碼 復制代碼
  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 選用何種游標?
顯示游標分為:普通游標,參數化游標和游標變量三種。
下面以一個過程來進行說明

Java代碼 復制代碼
  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
這一句是定義了一個最普通的游標,把整個查詢已經寫死,調用時不可以作任何改變。
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編程時,經常需要循環讀取結果集的數據。進行逐行處理,這個過程就需要對游標進行循環。對游標進行循環的方法有多種,我們在此一一分析。

Java代碼 復制代碼
  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;
  


使用游標之前需要開打游標,open cursor,循環完后再關閉游標close cursor.
這是使用游標應該慎記于心的法則。
上面的過程演示了游標循環的三種方法。
在討論循環方法之前,我們先看看%found和%notfound這些游標的屬性。

Java代碼 復制代碼
  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;
  

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

第一種使用loop 循環

Java代碼 復制代碼
  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之后。必避免多余的數據處理。
處理邏輯需要跟在exit when之后。這一點需要多加小心。
循環結束后要記得關閉游標。

第二種使用while循環。

Java代碼 復制代碼
  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;
  


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

第三種 for循環

Java代碼 復制代碼
  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循環是比較簡單實用的方法。
首先,它會自動open和close游標。解決了你忘記打開或關閉游標的煩惱。
其它,自動定義了一個記錄類型及聲明該類型的變量,并自動fetch數據到這個變量中。
我們需要注意v_pos 這個變量無需要在循環外進行聲明,無需要為其指定數據類型。
它應該是一個記錄類型,具體的結構是由游標決定的。
這個變量的作用域僅僅是在循環體內。
把v_pos看作一個記錄變量就可以了,如果要獲得某一個值就像調用記錄一樣就可以了。
如v_pos.pos_type
由此可見,for循環是用來循環游標的最好方法。高效,簡潔,安全。
但遺憾的是,常常見到的卻是第一種方法。所以從今之后得改變這個習慣了。

存儲過程常用技巧2


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 午夜影院福利 | 亚洲精品国产成人专区 | 免费观看a级完整视频 | 亚洲最大成人综合网 | 一本久久精品一区二区 | 免费视频日韩 | 射婷婷 | 国产黄色自拍 | 日本欧美大片 | 日韩视频 中文字幕 | 综合网色 | 曰本女人一级毛片看一级毛 | www.日本色 | 欧美亚洲国产日韩一区二区三区 | 久久成人免费视频 | 日韩国产午夜一区二区三区 | 欧美xvideosexo另类 | 国产又黄又a又潮娇喘视频 国产又色又爽又黄又刺激18 | 国产欧美精品一区二区 | 亚洲欧美日本一区 | 国产成人高清亚洲一区久久 | 日本精品一区 | 色综合九九 | 国产视频久 | 四虎在线精品 | 奇米影视222| 182午夜在线观看 | 亚洲高清免费在线观看 | 久久亚洲精品国产精品婷婷 | 无人码一区二区三区视频 | 成熟的女性强烈交性视频 | 99re6在线视频免费精品 | 成人xxxx | 欧美成人免费一区在线播放 | 黄色操视频| 日韩视频免费一区二区三区 | 很狠操| 热久久99影院 | 久久这里只有精品久久 | 久久亚洲这里只有精品18 | 婷婷在线免费观看 |