Oracle显示游标的使用及游标for循环


当前第2页 返回上一页

1、cursor游标使用

/*简单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 
  --定义游标并且赋值(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; 
 / 
 
杨过 
郭靖 
付政委 
刘自飞 
江风 
任我行 
任盈盈 
令狐冲 
韦一笑 
张无忌 
朵儿 
谢逊 
小龙女 
欧阳锋 
欧阳锋 

2、sys_refcursor游标使用

/* 
 *游标名: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> /* 
  *游标名: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; 
 / 
杨过 保护小龙女 
郭靖 修炼降龙十八掌 
付政委 看小人书 
刘自飞 编程写代码 
江风 编程写代码 
任我行 修炼神功 
任盈盈 游山玩水 
令狐冲 行侠仗义 
韦一笑 吸拾人雪 
张无忌 修行 
朵儿 洗浴 
谢逊 毕生研究屠龙刀 
小龙女 修炼玉女心经 
欧阳锋 看小人书 

补充一种循环条件

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 
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 collect for*/ 
declare 
cursor myemp_cur 
is select * from myemp; 
type myemp_tab is table of myemp%rowtype; 
myemp_rd myemp_tab; 
begin 
 open myemp_cur; 
 loop 
 fetch myemp_cur bulk collect into myemp_rd limit 20; 
 for i in 1..myemp_rd.count loop 
  dbms_output.put_line('姓名:'||myemp_rd(i).ename); 
 end loop; 
 exit when myemp_cur%notfound; 
 end loop; 
end; 

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...