PLSQL學習-【3迴圈結構】

哎呀我的天吶發表於2014-11-19

分批提交例子:

點選( 此處 )摺疊或開啟

  1. 13 : 30 : 11 SQL > declare

  2. 13 : 35 : 03   2  cursor cu_emp is select * from emp1 ;

  3. 13 : 35 : 22   3 begin

  4. 13 : 35 : 27   4 for v_cu in cu_emp loop

  5. 13 : 35 : 57   5       if v_cu . ename = \ 'haha\' then

  6. 13 : 36 : 24   6       delete from emp where empno = v_cu . empno ;

  7. 13 : 36 : 58   7       end if ;

  8. 13 : 37 : 01   8       if mod ( cu_emp % rowcount , 1000 ) = 0 then

  9. 13 : 37 : 34   9         dbms_output . put_line ( cu_emp % rowcount ) ;

  10. 13 : 38 : 03  10         commit ;

  11. 13 : 38 : 07  11       end if ;

  12. 13 : 38 : 11  12 end loop ;

  13. 13 : 38 : 20  13 -- commit;

  14. 13 : 38 : 29  14 end ;

  15. 13 : 38 : 33  15 /


  16. PL / SQL procedure successfully completed .


  17. Elapsed : 00 : 01 : 55 . 25


CLERK部門的員工,津貼變成200:

點選( 此處 )摺疊或開啟

  1. 10 : 32 : 59 SQL > declare

  2. 10 : 33 : 11   2  cursor cu_emp is select * from emp ;

  3. 10 : 33 : 23   3 begin

  4. 10 : 33 : 24   4       for i in cu_emp loop

  5. 10 : 33 : 35   5       if i . job = \ 'CLERK\' then

  6. 10 : 33 : 51   6           update emp set comm = 200 where empno = i . empno ;

  7. 10 : 34 : 11   7       end if ;

  8. 10 : 34 : 19   8       end loop ;

  9. 10 : 34 : 24   9       --commit;

  10. 10 : 34 : 30  10 end ;

  11. 10 : 34 : 32  11 /


  12. PL / SQL procedure successfully completed .


  13. Elapsed : 00 : 00 : 00 . 07


點選( 此處 )摺疊或開啟

  1. 10 : 38 : 21 SQL > declare

  2. 10 : 39 : 42   2      cursor cu_emp is select * from emp ;

  3. 10 : 39 : 53   3 begin

  4. 10 : 39 : 57   4       for i in cu_emp loop

  5. 10 : 40 : 07   5         if i . job = \ 'CLERK\' then

  6. 10 : 40 : 44   6            update emp set comm = 200 where empno = i . empno ;

  7. 10 : 41 : 47   7        elsif i . job = \ 'SALESMAN\' then                                    --日語錢包:saiif  發音很像

  8. 10 : 42 : 39   8            update emp set comm = 1000 where empno = i . empno ;

  9. 10 : 43 : 02   9         end if ;

  10. 10 : 43 : 11  10       end loop ;

  11. 10 : 43 : 15  11 end ;

  12. 10 : 43 : 22  12 /


  13. PL / SQL procedure successfully completed .

第一次執行時間102s:

第二次執行耗時很短:



三種迴圈:
    
 loop:

點選( 此處 )摺疊或開啟

  1. declare

  2.   i number ;

  3. begin

  4.     i : = 1 ;

  5.     loop

  6.        insert into emp ( empno , ename ) values ( i , \ 'YAO\' | | i ) ;

  7.       i : = i + 1 ;

  8.        exit when i > 2000 ;

  9.      end loop ;

  10.    commit ;

  11. end ;

while:

點選( 此處 )摺疊或開啟

  1. declare

  2.   i number ;

  3. begin

  4.     i : = 3000 ;

  5.     while i < = 6000 loop

  6.        insert into emp ( empno , ename ) values ( i , \ 'YAO\' | | i ) ;

  7.       i : = i + 1 ;

  8.      end loop ;

  9.    commit ;

  10. end ;


for迴圈:

點選( 此處 )摺疊或開啟

  1. declare

  2.   begin

  3.     for i in 8004 . . 9999 loop

  4.       insert into emp ( empno , ename ) values ( i , \ 'LOU\' ) ;

  5.     end loop ;

  6.  commit ;

  7.   end ;


迴圈巢狀:
    列印9 9 乘法表
    

點選( 此處 )摺疊或開啟

  1. 12 : 59 : 19 SQL > declare

  2. 12 : 59 : 26   2       begin

  3. 12 : 59 : 29   3       for i in 1 . . 9 loop

  4. 12 : 59 : 38   4       for j in 1 . . 9 loop

  5. 12 : 59 : 54   5          dbms_output . put_line ( i | | \ '*\' | | j | | \ '=\' | | i * j ) ;

  6. 13 : 01 : 33   6       end loop ;

  7. 13 : 01 : 42   7       end loop ;

  8. 13 : 01 : 46   8 end ;

  9. 13 : 01 : 49   9 /


  10. PL / SQL procedure successfully completed .


  11. Elapsed : 00 : 00 : 00 . 01

  12. 13 : 01 : 50 SQL > set serveroutput on

  13. 13 : 01 : 58 SQL > /

  14. 1 * 1 = 1

  15. 1 * 2 = 2

  16. 1 * 3 = 3

  17. 1 * 4 = 4

  18. 1 * 5 = 5

  19. 1 * 6 = 6

  20. 1 * 7 = 7

  21. 1 * 8 = 8

  22. 1 * 9 = 9

  23. 2 * 1 = 2

  24. 2 * 2 = 4

  25. 2 * 3 = 6

  26. 2 * 4 = 8

  27. 2 * 5 = 10

  28. 2 * 6 = 12

  29. 2 * 7 = 14

  30. 2 * 8 = 16

  31. 2 * 9 = 18

  32. 3 * 1 = 3

  33. 3 * 2 = 6

  34. 3 * 3 = 9

  35. 3 * 4 = 12

  36. 3 * 5 = 15

  37. 3 * 6 = 18

  38. 3 * 7 = 21

  39. 3 * 8 = 24

  40. 3 * 9 = 27

  41. 4 * 1 = 4

  42. 4 * 2 = 8

  43. 4 * 3 = 12

  44. 4 * 4 = 16

  45. 4 * 5 = 20

  46. 4 * 6 = 24

  47. 4 * 7 = 28

  48. 4 * 8 = 32

  49. 4 * 9 = 36

  50. 5 * 1 = 5

  51. 5 * 2 = 10

  52. 5 * 3 = 15

  53. 5 * 4 = 20

  54. 5 * 5 = 25

  55. 5 * 6 = 30

  56. 5 * 7 = 35

  57. 5 * 8 = 40

  58. 5 * 9 = 45

  59. 6 * 1 = 6

  60. 6 * 2 = 12

  61. 6 * 3 = 18

  62. 6 * 4 = 24

  63. 6 * 5 = 30

  64. 6 * 6 = 36

  65. 6 * 7 = 42

  66. 6 * 8 = 48

  67. 6 * 9 = 54

  68. 7 * 1 = 7

  69. 7 * 2 = 14

  70. 7 * 3 = 21

  71. 7 * 4 = 28

  72. 7 * 5 = 35

  73. 7 * 6 = 42

  74. 7 * 7 = 49

  75. 7 * 8 = 56

  76. 7 * 9 = 63

  77. 8 * 1 = 8

  78. 8 * 2 = 16

  79. 8 * 3 = 24

  80. 8 * 4 = 32

  81. 8 * 5 = 40

  82. 8 * 6 = 48

  83. 8 * 7 = 56

  84. 8 * 8 = 64

  85. 8 * 9 = 72

  86. 9 * 1 = 9

  87. 9 * 2 = 18

  88. 9 * 3 = 27

  89. 9 * 4 = 36

  90. 9 * 5 = 45

  91. 9 * 6 = 54

  92. 9 * 7 = 63

  93. 9 * 8 = 72

  94. 9 * 9 = 81


  95. PL / SQL procedure successfully completed .


迴圈退出:

點選( 此處 )摺疊或開啟

  1. declare

  2. begin

  3.    < < outer > >                 --標籤

  4.    for i in 1 . . 9 loop

  5.      < < inner > >

  6.      for j in 1 . . 9 loop

  7.        exit  outer when j > 5 ;

  8.       dbms_output . put_line ( i | | \ '*\' | | j | | \ '=\' | | i * j ) ;

  9.      end loop ;

  10.    end loop ;

  11. end ;


直接調到外層:

點選( 此處 )摺疊或開啟

  1. declare

  2. begin

  3.    < < outer > >

  4.    for i in 1 . . 9 loop

  5.      < < inner > >

  6.      for j in 1 . . 9 loop

  7.        exit outer when j > 5 ;

  8.       dbms_output . put_line ( i | | \ '*\' | | j | | \ '=\' | | i * j ) ;

  9.      end loop ;

  10.    end loop ;

  11. end ;


  12. 1 * 1 = 1

  13. 1 * 2 = 2

  14. 1 * 3 = 3

  15. 1 * 4 = 4

  16. 1 * 5 = 5



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29990276/viewspace-1339263/,如需轉載,請註明出處,否則將追究法律責任。

相關文章