oracle存储过程及函数中返回结果集(游标)
我建了一个包,包里一个存储过程一个函数,都是以SCOTT这个示例下的EMP表为例的,然后再使用匿名块对其进行调用并返回需要的结果。包里有另外多一个返回结果为数组类型的有一些问题,我们后面再继续讨论
包的内容如下:
create or replace package pkg_test1
as
type myrctype is ref cursor;
procedure get_emp(i_deptno emp.deptno%type,p_rc out myrctype);
function fun_emp(i_deptno emp.deptno%type) return myrctype;
type empinfo is record(
empno emp.empno%type,
ename emp.ename%type,
deptno emp.deptno%type);
type emp_data is table of empinfo;
procedure get_row(i_empno emp.empno%type,aa out emp_data);
end pkg_test1;
create or replace package body pkg_test1
as
procedure get_emp(i_deptno emp.deptno%type,p_rc out myrctype)
is
sqlstr varchar2(500);
begin
if i_deptno=0 then
open p_rc for select * from emp;
else
sqlstr := 'select * from emp where deptno = :i_deptno';
open p_rc for sqlstr using i_deptno;
end if;
end get_emp;
function fun_emp(i_deptno emp.deptno%type) return myrctype
is
sqlstr varchar2(500);
rc myrctype;
begin
if i_deptno=0 then
open rc for select * from emp;
else
sqlstr := 'select * from emp where deptno = :i_deptno';
open rc for sqlstr using i_deptno;
end if;
return rc;
end fun_emp;
procedure get_row(i_empno emp.empno%type,aa out emp_data)
is
begin
select empno,ename,deptno bulk collect into aa from emp where empno=i_empno;
/*for i in aa.first..aa.last loop
dbms_output.put_line(aa(i).empno||aa(i).ename||aa(i).deptno);
end loop;*/
end get_row;
end pkg_test1;
后面调用:
declare
aa sys_refcursor;
bb sys_refcursor;
lrow emp%rowtype;
begin
aa := pkg_test1.fun_emp(20);
pkg_test1.get_emp(10,bb);
loop
exit when aa%notfound;
FETCH aa INTO lrow;
DBMS_OUTPUT.put_line (lrow.empno||' '||lrow.ename);
end loop;
CLOSE aa; ---bb一样用此方法显示这些数据
loop
exit when bb%notfound;
FETCH bb INTO lrow;
DBMS_OUTPUT.put_line (lrow.empno||' '||lrow.ename);
end loop;
CLOSE bb;
end;
但是在匿名块中返回使用RECORD类型定义出来的多维数组类型,在调用的时候是不成功的,后来看到ITPUB里有个大哥的一段话,了解了,给出网址,供大家学习参考
http://www.itpub.net/thread-1326987-1-1.html,加油学习,嘿嘿~~~![](https://www.cnblogs.com/Emoticons/yoyocici/224025817.gif)