手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle游标cursor简单使用

[日期:2012-08-24] 来源:Linux社区  作者:yao__shun__yu [字体:大 中 小]

总共介绍两种游标 cursor 与 sys_refcursor

1、cursor游标使用

  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;      
执行结果
  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. 欧阳锋  
  40. 欧阳锋  

2、sys_refcursor游标使用

  1. /*  
  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;  
执行结果
  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. 小龙女 修炼玉女心经  
  37. 欧阳锋 看小人书  
补充一种循环条件
  1. declare  
  2.      
  3.    stu_cur sys_refcursor;  
  4.    stuone students%rowtype;  
  5.      
  6.    begin  
  7.      open stu_cur for select * from students;  
  8.      fetch stu_cur into stuone;  
  9.      --特别注意循环条件的改变   
  10.      --这个条件是发现了在循环   
  11.      --与上一个notfound不同的   
  12.      while stu_cur%found loop   
  13.        dbms_output.put_line(stuone.name||' '||stuone.hobby);  
  14.        fetch stu_cur into stuone;  
  15.      end loop;  
  16.    end;    
linux
相关资讯       Oracle游标 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款