Oracle入门之函数返回游标
在Oracle中,函数的返回值可以是游标。游标变量的类型是sys_refcursor。定义函数时,返回值类型是sys_refcursor,在函数中声明一个sys_refcursor的变量,返回该变量。
·
函数返回游标
在Oracle中,函数的返回值可以是游标。游标变量的类型是sys_refcursor。
定义函数时,返回值类型是sys_refcursor,在函数中声明一个sys_refcursor的变量,返回该变量。
示例
假如我们有一个员工表,现在创建函数将部门号为30的员工信息全部返回。
编写函数:
create or replace function ff return sys_refcursor is
--定义一个游标变量
res sys_refcursor;
begin
--打开游标变量,让游标变量指向我们要查询的结果集
open res for select * from emp where deptno=30;
--返回游标变量
return res;
end;
调用函数
declare
cc sys_refcursor; --定义一个游标变量接收函数返回值
row emp%rowtype;
begin
--注:这里不需要打开变量,因为已经在函数中打开过了
cc:=ff; --调用函数
loop
--提取变量
fetch cc into row;
exit when cc%notfound;
dbms_output.put_line(row.ename);--打印员工表里面的员工姓名
end loop;
--关闭游标
close cc;
end;
更多推荐
所有评论(0)