经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Oracle » 查看文章
oracle学习笔记(二十二) REF 动态游标 - Stars-one
来源:cnblogs  作者:Stars-one  时间:2019/6/4 15:40:38  对本文有异议

动态游标

定义语法

  1. --声明
  2. $cursor_name$ sys_refcursor
  3. --打开动态游标
  4. open $cursor_name$ is 查询语句;
  5. --关闭游标
  6. close $cursor_name$;
  7. --声明动态游标类型
  8. type $type_name$ is ref cursor;
  9. --声明一个动态游标变量
  10. $v_cursor_name$ type_my_ref;

使用

动态游标可以获得不同的结果集,可以设置条件,返回不同的结果集,一般和过程一起使用

  1. --创建过程list
  2. create or replace procedure list(result_set in out sys_refcursor, which in number)
  3. is
  4. begin
  5. --打开动态游标时在为它指定查询语句
  6. --1就是返回员工表,其他就返回部门表
  7. if which=1 then
  8. open result_set for select * from employee;
  9. else
  10. open result_set for select * from department;
  11. end if;
  12. end;
  13. /
  14. --调用list过程,根据输入的数字输出某个表的全部信息
  15. declare
  16. --声明一个动态游标类型
  17. type type_my_ref is ref cursor;
  18. --声明一个动态游标变量
  19. my_ref type_my_ref;
  20. emp employee%rowtype;
  21. dept department%rowtype;
  22. which number default &请输入;
  23. begin
  24. --得到一个查询结果集
  25. list(my_ref, which);
  26. loop
  27. if which=1 then /* handle employee */
  28. fetch my_ref into emp;
  29. exit when my_ref%notfound;
  30. dbms_output.put_line(emp.empno||','||emp.ename||','||emp.job||','||emp.sal);
  31. else /* handle department */
  32. fetch my_ref into dept;
  33. exit when my_ref%notfound;
  34. dbms_output.put_line(dept.deptno||','||dept.dname||','||dept.loc);
  35. end if;
  36. end loop;
  37. --close cursor
  38. close my_ref;
  39. end;
  40. /

原文链接:http://www.cnblogs.com/kexing/p/10970095.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号