oracle学习笔记(二十二) REF 动态游标

mac2022-06-30  87

动态游标

定义语法

复制--声明 $cursor_name$ sys_refcursor --打开动态游标 open $cursor_name$ is 查询语句; --关闭游标 close $cursor_name$; --声明动态游标类型 type $type_name$ is ref cursor; --声明一个动态游标变量 $v_cursor_name$ type_my_ref;

使用

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

复制--创建过程list create or replace procedure list(result_set in out sys_refcursor, which in number) is begin --打开动态游标时在为它指定查询语句 --1就是返回员工表,其他就返回部门表 if which=1 then open result_set for select * from employee; else open result_set for select * from department; end if; end; / --调用list过程,根据输入的数字输出某个表的全部信息 declare --声明一个动态游标类型 type type_my_ref is ref cursor; --声明一个动态游标变量 my_ref type_my_ref; emp employee%rowtype; dept department%rowtype; which number default &请输入; begin --得到一个查询结果集 list(my_ref, which); loop if which=1 then /* handle employee */ fetch my_ref into emp; exit when my_ref%notfound; dbms_output.put_line(emp.empno||','||emp.ename||','||emp.job||','||emp.sal); else /* handle department */ fetch my_ref into dept; exit when my_ref%notfound; dbms_output.put_line(dept.deptno||','||dept.dname||','||dept.loc); end if; end loop; --close cursor close my_ref; end; /

转载于:https://www.cnblogs.com/chaoyang123/p/11549624.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)