Simple use of oracle cursor

Source: Internet
Author: User


Oracle cursor simple use a total of two types of cursor and sys_refcursor 1. The cursor uses the name field in the [SQL]/* simple cursor * students table, you can perform other table tests */-- Define declare -- Define the cursor and assign values (is cannot be used separately from cursor) cursor stus_cur is select * from students; -- Define rowtype cur_stu students % rowtype;/* start to execute */begin -- enable the cursor open stus_cur; -- loop condition exit when stus_cur % notfound; -- The cursor value is assigned to rowtype fetch stus_cur into cur_stu; -- outputs www.2cto.com dbms_output.put_line (cur_stu.name); -- ends the loop end loop; -- closes the cursor close stus_cur;/* ends execution; execution result [SQL] SQL> declare 2 -- Define the cursor and assign values (is cannot be used separately from cursor) 3 cursor stus_cur is select * from students; 4 -- Define rowtype 5 cur_stu students % rowtype; 6/* start execution */7 begin www.2cto.com 8 -- enable cursor 9 open stus_cur; 10 -- loop 11 loop 12 -- loop condition 13 exit when stus_cur % notfound; 14 -- The 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 loop 19 end loop; 20 -- close cursor 21 close stus_cur; 22/* end execution */23 end; 24/Yang Guo Jing Fu Political Commissar Liu zifei Jiang Feng Ren Ying Ling Hu Chong Wei Yi Xiao Zhang Wuji duo Xie Xun Xiaolong female Ouyang Feng 2, sys_refcursor cursor using www.2cto.com [SQL]/** cursor Name: sys_refcursor * pay special attention to the Assignment Method: for * and the duplicate content are not described */declare stu_cur sys_refcursor; stuone students % rowtype; begin -- the assignment method is for open stu_cur for select * from students; -- fetch is assigned to rowtype fetch stu_cur into stuone; loop dbms_output.put_line (stuone. name | ''| stuone. holobby); fetch stu_cur into stuone; exit when stu_cur % notfound; end loop; end; execution result [SQL] SQL>/* 2 * cursor Name: sys_refcursor 3 * pay special attention to the Assignment Method: for 4 * does not describe 5 */www.2cto.com 6 declare 7 stu_cur sys_refcursor; 8 stuone students % rowtype; 9 10 begin 11 -- the assignment method is for 12 open stu_cur for select * from students; 13 -- fetch is assigned to rowtype 14 fetch stu_cur into stuone; 15 16 loop 17 dbms_output.put_line (stuone. name | ''| stuone. holobby); 18 fetch stu_cur into stuone; 19 exit when stu_cur % notfound; 20 end loop; 21 end; 22/Yang once protected Xiaolong female Guo Jing cultivation Xiaolong 18 ops Fu Political Commissar to read the little man's book Liu zifei programming to write code Jiangfeng programming to write code for me to cultivate the magic of Ren Yingying mountain playings Ling Hu Chong Xia Yi Wei Yi smile pick up people snow bath Xie Xun's life-long study of the dragon knife little dragon and female cultivation Yu nvxin by Ouyang Feng to read the little man book www.2cto.com to add a cycle condition [SQL] declare stu_cur sys_refcursor; stuone students % rowtype; begin open stu_cur for select * from students; fetch stu_cur into stuone; -- pay special attention to the change of the cyclic condition -- this condition is found in the loop -- different from the previous notfound while stu_cur % found loop dbms_output.put_line (stuone. name | ''| stuone. holobby); fetch stu_cur into stuone; end loop; end;
 

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.