oracle資料庫中的cursor

來源:互聯網
上載者:User

標籤:style   http   color   os   使用   ar   strong   for   資料   

總共介紹兩種遊標一種高效使用遊標cursor 、sys_refcursor、 bulk collect

1、cursor遊標使用

[sql] view plain copy print ?
  1. /*簡單cursor遊標
  2. *students表裡面有name欄位,你可以換做其他表測試
  3. */
  4. --定義
  5. declare
  6. --定義遊標並且賦值(is 不能和cursor分開使用)
  7. cursor stus_cur is select * from students;
  8. --定義rowtype
  9. cur_stu students%rowtype;
  10. /*開始執行*/
  11. begin
  12. --開啟遊標
  13. open stus_cur;
  14. --loop迴圈
  15. loop
  16. --迴圈條件
  17. exit when stus_cur%notfound;
  18. --遊標值賦值到rowtype
  19. fetch stus_cur into cur_stu;
  20. --輸出
  21. dbms_output.put_line(cur_stu.name);
  22. --結束迴圈
  23. end loop;
  24. --關閉遊標
  25. close stus_cur;
  26. /*結束執行*/
  27. end;
/*簡單cursor遊標 *students表裡面有name欄位,你可以換做其他表測試 */--定義declare --定義遊標並且賦值(is 不能和cursor分開使用) cursor stus_cur is select * from students; --定義rowtype cur_stu students%rowtype; /*開始執行*/ begin   --開啟遊標   open stus_cur;      --loop迴圈      loop         --迴圈條件        exit when stus_cur%notfound;        --遊標值賦值到rowtype        fetch stus_cur into cur_stu;        --輸出        dbms_output.put_line(cur_stu.name);      --結束迴圈        end loop;    --關閉遊標     close stus_cur;  /*結束執行*/ end;

執行結果

[sql] view plain copy print ?
  1. SQL> declare
  2. 2 --定義遊標並且賦值(is 不能和cursor分開使用)
  3. 3 cursor stus_cur is select * from students;
  4. 4 --定義rowtype
  5. 5 cur_stu students%rowtype;
  6. 6 /*開始執行*/
  7. 7 begin
  8. 8 --開啟遊標
  9. 9 open stus_cur;
  10. 10 --loop迴圈
  11. 11 loop
  12. 12 --迴圈條件
  13. 13 exit when stus_cur%notfound;
  14. 14 --遊標值賦值到rowtype
  15. 15 fetch stus_cur into cur_stu;
  16. 16 --輸出
  17. 17 dbms_output.put_line(cur_stu.name);
  18. 18 --結束迴圈
  19. 19 end loop;
  20. 20 --關閉遊標
  21. 21 close stus_cur;
  22. 22 /*結束執行*/
  23. 23 end;
  24. 24 /
  25. 楊過
  26. 郭靖
  27. 付政委
  28. 劉自飛
  29. 江風
  30. 任我行
  31. 任盈盈
  32. 令狐沖
  33. 韋一笑
  34. 張無忌
  35. 朵兒
  36. 謝遜
  37. 小龍女
  38. 歐陽鋒
  39. 歐陽鋒
SQL> declare  2   --定義遊標並且賦值(is 不能和cursor分開使用)  3   cursor stus_cur is select * from students;  4   --定義rowtype  5   cur_stu students%rowtype;  6   /*開始執行*/  7   begin  8     --開啟遊標  9     open stus_cur; 10        --loop迴圈 11        loop 12          --迴圈條件 13          exit when stus_cur%notfound; 14          --遊標值賦值到rowtype 15          fetch stus_cur into cur_stu; 16          --輸出 17          dbms_output.put_line(cur_stu.name); 18        --結束迴圈 19        end loop; 20      --關閉遊標 21     close stus_cur; 22    /*結束執行*/ 23   end; 24  / 楊過郭靖付政委劉自飛江風任我行任盈盈令狐沖韋一笑張無忌朵兒謝遜小龍女歐陽鋒歐陽鋒

2、sys_refcursor遊標使用

[sql] view plain copy print ?
  1. /*
  2. *遊標名:sys_refcursor
  3. *特別注意賦值方式:for
  4. *與上重複內容不在敘述
  5. */
  6. declare
  7. stu_cur sys_refcursor;
  8. stuone students%rowtype;
  9. begin
  10. --這句賦值方式for
  11. open stu_cur for select * from students;
  12. --fetch賦值給rowtype
  13. fetch stu_cur into stuone;
  14. loop
  15. dbms_output.put_line(stuone.name||‘ ‘||stuone.hobby);
  16. fetch stu_cur into stuone;
  17. exit when stu_cur%notfound;
  18. end loop;
  19. end;
/* *遊標名:sys_refcursor *特別注意賦值方式:for *與上重複內容不在敘述 */declare   stu_cur sys_refcursor;   stuone students%rowtype;      begin     --這句賦值方式for     open stu_cur for select * from students;     --fetch賦值給rowtype     fetch stu_cur into stuone;          loop        dbms_output.put_line(stuone.name||‘ ‘||stuone.hobby);       fetch stu_cur into stuone;       exit when stu_cur%notfound;     end loop;   end;


執行結果

[sql] view plain copy print ?
  1. SQL> /*
  2. 2 *遊標名:sys_refcursor
  3. 3 *特別注意賦值方式:for
  4. 4 *與上重複內容不在敘述
  5. 5 */
  6. 6 declare
  7. 7 stu_cur sys_refcursor;
  8. 8 stuone students%rowtype;
  9. 9
  10. 10 begin
  11. 11 --這句賦值方式for
  12. 12 open stu_cur for select * from students;
  13. 13 --fetch賦值給rowtype
  14. 14 fetch stu_cur into stuone;
  15. 15
  16. 16 loop
  17. 17 dbms_output.put_line(stuone.name||‘ ‘||stuone.hobby);
  18. 18 fetch stu_cur into stuone;
  19. 19 exit when stu_cur%notfound;
  20. 20 end loop;
  21. 21 end;
  22. 22 /
  23. 楊過 保護小龍女
  24. 郭靖 修鍊降龍十八掌
  25. 付政委 看小人書
  26. 劉自飛 編程寫代碼
  27. 江風 編程寫代碼
  28. 任我行 修鍊神功
  29. 任盈盈 遊山玩水
  30. 令狐沖 行俠仗義
  31. 韋一笑 吸拾人雪
  32. 張無忌 修行
  33. 朵兒 洗浴
  34. 謝遜 畢生研究屠龍刀
  35. 小龍女 修鍊玉女心經
  36. 歐陽鋒 看小人書
SQL> /*  2   *遊標名:sys_refcursor  3   *特別注意賦值方式:for  4   *與上重複內容不在敘述  5   */  6  declare  7     stu_cur sys_refcursor;  8     stuone students%rowtype;  9   10     begin 11       --這句賦值方式for 12       open stu_cur for select * from students; 13       --fetch賦值給rowtype 14       fetch stu_cur into stuone; 15   16       loop 17         dbms_output.put_line(stuone.name||‘ ‘||stuone.hobby); 18         fetch stu_cur into stuone; 19         exit when stu_cur%notfound; 20       end loop; 21     end; 22  / 楊過 保護小龍女郭靖 修鍊降龍十八掌付政委 看小人書劉自飛 編程寫代碼江風 編程寫代碼任我行 修鍊神功任盈盈 遊山玩水令狐沖 行俠仗義韋一笑 吸拾人雪張無忌 修行朵兒 洗浴謝遜 畢生研究屠龍刀小龍女 修鍊玉女心經歐陽鋒 看小人書


補充一種迴圈條件

[sql] view plain copy print ?
  1. declare
  2. stu_cur sys_refcursor;
  3. stuone students%rowtype;
  4. begin
  5. open stu_cur for select * from students;
  6. fetch stu_cur into stuone;
  7. --特別注意迴圈條件的改變
  8. --這個條件是發現了在迴圈
  9. --與上一個notfound不同的
  10. while stu_cur%found loop
  11. dbms_output.put_line(stuone.name||‘ ‘||stuone.hobby);
  12. fetch stu_cur into stuone;
  13. end loop;
  14. end;
declare      stu_cur sys_refcursor;   stuone students%rowtype;      begin     open stu_cur for select * from students;     fetch stu_cur into stuone;     --特別注意迴圈條件的改變     --這個條件是發現了在迴圈     --與上一個notfound不同的     while stu_cur%found loop        dbms_output.put_line(stuone.name||‘ ‘||stuone.hobby);       fetch stu_cur into stuone;     end loop;   end;

--普通的fetch into

[sql] view plain copy print ?
  1. /*普通方式*/
  2. declare
  3. cursor myemp_cur is select * from myemp;
  4. v_myemp myemp%rowtype;
  5. begin
  6. open myemp_cur;
  7. fetch myemp_cur into v_myemp;
  8. while myemp_cur%found loop
  9. dbms_output.put_line(v_myemp.ename);
  10. fetch myemp_cur into v_myemp;
  11. end loop;
  12. end;
/*普通方式*/declarecursor myemp_cur is select * from myemp;v_myemp myemp%rowtype;begin  open myemp_cur;  fetch myemp_cur into v_myemp;   while myemp_cur%found loop    dbms_output.put_line(v_myemp.ename);    fetch myemp_cur into v_myemp;  end loop;end;


--高效的bulk collect

[sql] view plain copy print ?
  1. /*高效bulk collect for*/
  2. declare
  3. cursor myemp_cur
  4. is select * from myemp;
  5. type myemp_tab is table of myemp%rowtype;
  6. myemp_rd myemp_tab;
  7. begin
  8. open myemp_cur;
  9. loop
  10. fetch myemp_cur bulk collect into myemp_rd limit 20;
  11. for i in 1..myemp_rd.count loop
  12. dbms_output.put_line(‘姓名:‘||myemp_rd(i).ename);
  13. end loop;
  14. exit when myemp_cur%notfound;
  15. end loop;
  16. end;

oracle資料庫中的cursor

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.