Cursor in Oracle Database

Source: Internet
Author: User

A total of two cursors are described for an efficient use of cursors cursor ,sys_refcursor, bulk collect

1, cursor cursor use

[SQL]View Plain copy print?
  1. / * Simple cursor cursor
  2. There is a name field in the *students table, you can change the other table test
  3. */
  4. --Definition
  5. declare
  6. --Define cursor and assign value (is cannot be used separately from cursor)
  7. cursor stus_cur is select * from students;
  8. --Define RowType
  9. Cur_stu Students%rowtype;
  10. / * Start execution * /
  11. begin
  12. --Open cursor
  13. Open stus_cur;
  14. --loop cycle
  15. Loop
  16. --Cycle conditions
  17. exit when stus_cur%notfound;
  18. --Cursor value assignment to RowType
  19. fetch stus_cur into cur_stu;
  20. --Output
  21. dbms_output.put_line (cur_stu. Name);
  22. --end loop
  23. end loop;
  24. --close cursor
  25. close stus_cur;
  26. / * End execution * /
  27. end;
/* Simple cursor Cursor *students table contains the Name field, you can change the other table test */--definition Declare--Define the cursor and assign the value (is cannot be used separately from the cursor) cursor stus_cur is select * from S tudents; --Define RowType cur_stu Students%rowtype; /* Start execution */BEGIN   -Open cursor   open stus_cur;      --loop loop      Loops         --loop condition        exit when Stus_cur%notfound;        --The value of the cursor is assigned to RowType        fetch stus_cur into Cur_stu;        --Output        dbms_output.put_line (cur_stu.name);      --end loop        ;    --Closing the cursor close     stus_cur;  /* Finish execution */end;

Execution Results

[SQL]View Plain copy print?
  1. sql> declare
  2. 2 --Define cursor and assign value (is cannot be used separately from cursor)
  3. 3 cursor stus_cur is select * from students;
  4. 4 --Define RowType
  5. 5 cur_stu Students%rowtype;
  6. 6/* Start execution * /
  7. 7 begin
  8. 8 --Open cursor
  9. 9 Open stus_cur;
  10. Ten --loop cycle
  11. Loop
  12. -- cyclic conditions
  13. exit when stus_cur%notfound;
  14. -- cursor value is assigned to RowType
  15. fetch stus_cur into cur_stu;
  16. --Output
  17. dbms_output.put_line (cur_stu. Name);
  18. - -end cycle
  19. end loop;
  20. - -Close the cursor
  21. close stus_cur;
  22. 22/* End execution * /
  23. The end;
  24. /
  25. Yang too
  26. Guo Jing
  27. pay the Commissar .
  28. Liuzhifei
  29. Jiang Feng
  30. let me do it.
  31. Ren Ying
  32. make the fox rush
  33. Wei a Smile
  34. Zhang Mowgli
  35. Dawn
  36. Sheson
  37. Little Dragon Girl
  38. Ouyang Fung
  39. Ouyang Fung
Sql> declare  2   --Define a cursor and assign a value (is cannot be used separately from the cursor)  3   cursor Stus_cur is a SELECT * from students;  4   --Define RowType  5   cur_stu students%rowtype;  6   /* Start execution */  7   begin  8     --Open cursor  9     open stus_cur;        --loop Loop        one Loop 12          --Cycle condition of          exit when Stus_cur%notfound;-          -the cursor value is assigned to RowType          fetch stus_cur into Cur_stu;          --Output          dbms_output.put_line (cur_stu.name);--        end loop        ;      Close     Stus_ Cur    */* End execution *   /finish;  /Yang over Guo Jingxian commissar Liuzhifei Jiang Feng Ren ying to make Fox Chong Wei smile Zhang Mowgli Dawn Shesson female Ouyang Fung Ouyang Fung

2.sys_refcursor cursor Use

[SQL]View Plain copy print?
  1. /*
  2. * Cursor Name: Sys_refcursor
  3. * Pay special attention to the assignment method: for
  4. * With the above repeating content is not in the narrative
  5. */
  6. declare
  7. stu_cur sys_refcursor;
  8. Stuone Students%rowtype;
  9. begin
  10. --This sentence is assigned
  11. Open stu_cur for select * from students;
  12. --fetch assignment to 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;
/* Cursor Name: Sys_refcursor * Pay special attention to the assignment method: for * with the repetition of the content is not described */declare   stu_cur sys_refcursor;   Stuone Students%rowtype;      Begin     -This sentence is assigned in the form of     open stu_cur for select * from students;     --fetch assignment to 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


Execution Results

[SQL]View Plain copy print?
  1. sql>/*
  2. 2 * Cursor name: Sys_refcursor
  3. 3 * Pay special attention to the assignment method: for
  4. 4 * With above repeating content not in narration
  5. 5 * /
  6. 6 Declare
  7. 7 stu_cur sys_refcursor;
  8. 8 Stuone Students%rowtype;
  9. 9
  10. ten begin
  11. one --the value of this sentence for
  12. Open stu_cur for SELECT * from students;
  13. --fetch assignment to RowType
  14. fetch stu_cur into stuone;
  15. the
  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. The end;
  22. /
  23. Yang over protect the little Dragon girl
  24. Guo Jing cultivation of descending dragon 18 Palm
  25. pay the Commissar to see picture
  26. Liuzhifei Programming Write code
  27. Jiang Feng Programming code writing
  28. Let me do the work of practicing.
  29. Ren Ying sightseeing Tour
  30. To make the fox rush the man.
  31. Wei a smile to suck up people snow
  32. Zhang Mowgli Practice
  33. Dawn Bath
  34. Sheson lifelong study of dragon Slayer Sword
  35. Dragon girl cultivation herself Heart Sutra
  36. Ouyang Fung See picture
sql>/*  2   * cursor name: sys_refcursor  3   * Pay special attention to the assignment method:  for 4   * with the above repeating content is not in narrative  5 */  6  DECLARE  7     stu_cur sys_refcursor;  8     stuone Students%rowtype;  9     begin one       -this sentence is assigned for the       open stu_cur for select * from students;       --fetch to RowType 14< C20/>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;  /Yang over protect dragon girl Guo Jing Cultivation of the Dragon 18 to pay the Commissar to see picture Liuzhifei programming code to write code Jiang Wind program to write codes for any of my line of cultivation of the sword to make a visit to the Fox rushed to the Knight Righteousness Wei a smile suction people snow Zhang Mowgli practice Dawn Bath Sheson lifelong research Dragon knife long female cultivation herself heart by Ouyang Fung See picture


supplement a cyclic condition

[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. --pay special attention to changes in cycle conditions
  8. --This condition is found in the loop
  9. --different from the previous 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;     --pay particular attention to the change in cyclic conditions     -this condition is found in the loop     -unlike the previous notfound while     Stu_cur%found loop        dbms_output.put_line ( stuone.name| | ' ' | | Stuone.hobby);       Fetch stu_cur into Stuone;     End Loop;   End

--normal fetch into

[SQL]View Plain copy print?
    1. /* Normal way */
    2. declare
    3. cursor myemp_cur is Select * from myemp;
    4. v_myemp myemp%rowtype;
    5. begin
    6. open m Yemp_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 ;
/* Normal way */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;


--efficient bulk collect

[SQL]View Plain copy print?
  1. / * Efficient 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;
  11. For i in 1..myemp_rd. Count Loop
  12. dbms_output.put_line (' name: '| | MYEMP_RD (i). ename);
  13. end loop;
  14. exit when myemp_cur%notfound;
  15. end loop;
  16. end;

Cursor in Oracle Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.