Oracle游标cursor简单使用

Linux大全评论553 views阅读模式

总共介绍两种游标 cursorsys_refcursor

  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. 欧阳锋  
  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;    

企鹅博客
  • 本文由 发表于 2019年9月6日 18:16:02
  • 转载请务必保留本文链接:https://www.qieseo.com/185965.html

发表评论