PL/SQL入門

Sql代碼 複製代碼
  1. SQL>set serveroutput on;  --顯示輸出   
  2.   
  3. -------------------------------------------------   
  4. --LOOP循環   
  5. declare  
  6.   i number:=1;   
  7. begin  
  8.   loop   
  9.     i:=i+1;   
  10.     exit when i=100;   
  11.     dbms_output.put_line(i);   
  12.   end loop;   
  13.     
  14. end;   
  15.   
  16. -------------------------------------------------   
  17. -- FOR LOOP循環50次   
  18. declare  
  19.   i number :=10;   
  20. begin  
  21.   for idx in 1..50 loop  --次數:50-1+1=50   
  22.     dbms_output.put_line(i);   
  23.   end loop;   
  24. end;   
  25.   
  26. -------------------------------------------------   
  27. -- WHILE LOOP循環   
  28. declare  
  29.   i number :=100;   
  30.   j number :=1;   
  31. begin  
  32.   while(j<i) loop   
  33.     dbms_output.put_line(j);   
  34.     j:=j+1;   
  35.   end loop;   
  36. end;   
  37.   
  38. -------------------------------------------------   
  39. --IF語句   
  40. declare  
  41.   i number :=40;   
  42.   j number :=10;   
  43. begin  
  44.   if(i<j) then  
  45.     dbms_output.put_line('OK');   
  46.   elsif (i=j) then  
  47.     dbms_output.put_line('OK1');   
  48.   else  
  49.     dbms_output.put_line('OK2');   
  50.   end if;   
  51. end;   
  52.   
  53.   
  54. =================================================   
  55. 遊標操作   
  56. =================================================   
  57.   
  58. --顯示遊標   
  59. declare  
  60.   --建立顯示遊標   
  61.   cursor csr_1 is  
  62.      select name from stu1;   
  63.   cursor csr_2 is  
  64.      select id from stu1;   
  65.   --定義變量   
  66.   str varchar2(10);   
  67.   str2 varchar2(10);   
  68. begin  
  69.   --打開遊標   
  70.   open csr_1;   
  71.   open csr_2;   
  72.   --附值   
  73.   fetch csr_1 into str;   
  74.   fetch csr_2 into str2;   
  75.   --輸出   
  76.   dbms_output.put_line(str);   
  77.   dbms_output.put_line(str2);   
  78. end;   
  79.   
  80. -------------------------------------------------   
  81. --取表某列全部記錄(適合控制)   
  82. declare  
  83.   --建立顯示遊標   
  84.   cursor csr_1 is  
  85.      select name,note from stu1;   
  86.   --定義變量   
  87.   str  varchar2(10);   
  88.   str2 varchar2(10);   
  89. begin  
  90.   --打開遊標   
  91.   open csr_1;   
  92.   loop   
  93.     fetch csr_1 into str,str2;   
  94.     exit when csr_1%notfound;  --到達記錄尾   
  95.     dbms_output.put_line(str || str2);   
  96.   end loop;   
  97.     
  98.   --關閉遊標   
  99.   close csr_1;   
  100. end;   
  101.   
  102. -------------------------------------------------   
  103. --FOR LOOP查詢表全部記錄(簡單--打開、附值、關閉隱式完成)   
  104. declare  
  105.   --建立顯示遊標   
  106.   cursor csr_1 is  
  107.      select name,note from stu1;   
  108. begin  
  109.   for idx in csr_1 loop   
  110.     dbms_output.put_line(idx.name || idx.note);   
  111.   end loop;   
  112. end;   
  113.   
  114.   
  115. -------------------------------------------------   
  116. --利用遊標複製表(效率不高)   
  117. declare  
  118.   cursor csr_1 is  
  119.      select ks_xm,ks_zkz,bk_cj from ahzk_all_cj;   
  120.   i number;   
  121. begin  
  122.   for idx in csr_1 loop   
  123.     insert into tmp values(idx.ks_xm,idx.ks_zkz,idx.bk_cj);   
  124.     i:=csr_1%rowcount;  --獲取當前記錄數   
  125.     if(i mod 1000=0) then  
  126.       dbms_output.put_line(i);   
  127.       commit;   
  128.     end if;      
  129.   end loop;   
  130.   commit;   
  131. end;   
  132.   
  133. --附:高效複製表   
  134. declare  
  135. begin  
  136.     insert into tmp   
  137.        select ks_xm,ks_zkz,bk_cj from ahzk_all_cj;   
  138.     if(sql%found) then  
  139.       dbms_output.put_line('插入' || sql%rowcount || '行記錄');   
  140.     end if;   
  141.     commit;   
  142. end;   
  143. ------------  
發佈了60 篇原創文章 · 獲贊 6 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章