oracle中的cousor,oracle数据库中的cursor

本文介绍了Oracle数据库中游标的两种常见使用方式:简单游标和sys_refcursor。通过示例展示了如何声明、打开、遍历和关闭游标,以及如何将游标值赋值给rowtype变量。在循环条件上,分别演示了使用notfound和found进行循环判断。此外,还提到了bulkcollect方法以提高处理大量数据的效率。
摘要由CSDN通过智能技术生成

总共介绍两种游标一种高效使用游标cursor 、sys_refcursor、 bulk collect

1、cursor游标使用

/*简单cursor游标

*students表里面有name字段,你可以换做其他表测试

*/

--定义

declare

--定义游标并且赋值(is 不能和cursor分开使用)

cursorstus_curisselect*fromstudents;

--定义rowtype

cur_stu students%rowtype;

/*开始执行*/

begin

--开启游标

openstus_cur;

--loop循环

loop

--循环条件

exit whenstus_cur%notfound;

--游标值赋值到rowtype

fetchstus_curintocur_stu;

--输出

dbms_output.put_line(cur_stu.name);

--结束循环

endloop;

--关闭游标

closestus_cur;

/*结束执行*/

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>declare

2 --定义游标并且赋值(is 不能和cursor分开使用)

3 cursorstus_curisselect*fromstudents;

4 --定义rowtype

5 cur_stu students%rowtype;

6 /*开始执行*/

7 begin

8 --开启游标

9 openstus_cur;

10 --loop循环

11 loop

12 --循环条件

13 exit whenstus_cur%notfound;

14 --游标值赋值到rowtype

15 fetchstus_curintocur_stu;

16 --输出

17 dbms_output.put_line(cur_stu.name);

18 --结束循环

19 endloop;

20 --关闭游标

21 closestus_cur;

22 /*结束执行*/

23 end;

24 /

杨过

郭靖

付政委

刘自飞

江风

任我行

任盈盈

令狐冲

韦一笑

张无忌

朵儿

谢逊

小龙女

欧阳锋

欧阳锋

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游标使用

/*

*游标名:sys_refcursor

*特别注意赋值方式:for

*与上重复内容不在叙述

*/

declare

stu_cur sys_refcursor;

stuone students%rowtype;

begin

--这句赋值方式for

openstu_curforselect*fromstudents;

--fetch赋值给rowtype

fetchstu_curintostuone;

loop

dbms_output.put_line(stuone.name||' '||stuone.hobby);

fetchstu_curintostuone;

exit whenstu_cur%notfound;

endloop;

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> /*

2 *游标名:sys_refcursor

3 *特别注意赋值方式:for

4 *与上重复内容不在叙述

5 */

6 declare

7 stu_cur sys_refcursor;

8 stuone students%rowtype;

9

10 begin

11 --这句赋值方式for

12 openstu_curforselect*fromstudents;

13 --fetch赋值给rowtype

14 fetchstu_curintostuone;

15

16 loop

17 dbms_output.put_line(stuone.name||' '||stuone.hobby);

18 fetchstu_curintostuone;

19 exit whenstu_cur%notfound;

20 endloop;

21 end;

22 /

杨过 保护小龙女

郭靖 修炼降龙十八掌

付政委 看小人书

刘自飞 编程写代码

江风 编程写代码

任我行 修炼神功

任盈盈 游山玩水

令狐冲 行侠仗义

韦一笑 吸拾人雪

张无忌 修行

朵儿 洗浴

谢逊 毕生研究屠龙刀

小龙女 修炼玉女心经

欧阳锋 看小人书

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 /

杨过 保护小龙女

郭靖 修炼降龙十八掌

付政委 看小人书

刘自飞 编程写代码

江风 编程写代码

任我行 修炼神功

任盈盈 游山玩水

令狐冲 行侠仗义

韦一笑 吸拾人雪

张无忌 修行

朵儿 洗浴

谢逊 毕生研究屠龙刀

小龙女 修炼玉女心经

欧阳锋 看小人书

补充一种循环条件

declare

stu_cur sys_refcursor;

stuone students%rowtype;

begin

openstu_curforselect*fromstudents;

fetchstu_curintostuone;

--特别注意循环条件的改变

--这个条件是发现了在循环

--与上一个notfound不同的

while stu_cur%found loop

dbms_output.put_line(stuone.name||' '||stuone.hobby);

fetchstu_curintostuone;

endloop;

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

/*普通方式*/

declare

cursormyemp_curisselect*frommyemp;

v_myemp myemp%rowtype;

begin

openmyemp_cur;

fetchmyemp_curintov_myemp;

while myemp_cur%found loop

dbms_output.put_line(v_myemp.ename);

fetchmyemp_curintov_myemp;

endloop;

end;

/*普通方式*/

declare

cursor 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

/*高效bulk collectfor*/

declare

cursormyemp_cur

isselect*frommyemp;

type myemp_tab istableofmyemp%rowtype;

myemp_rd myemp_tab;

begin

openmyemp_cur;

loop

fetchmyemp_cur bulk collectintomyemp_rd limit 20;

foriin1..myemp_rd.countloop

dbms_output.put_line('姓名:'||myemp_rd(i).ename);

endloop;

exit whenmyemp_cur%notfound;

endloop;

end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值