如何从存储过程返回多行? (Oracle PL / SQL)
时间:2020-03-06 14:25:28 来源:igfitidea点击:
我想用一个参数创建一个存储过程,该存储过程将根据参数返回不同的记录集。这是怎么做的?我可以从普通SQL中调用它吗?
解决方案
我认为我们想返回一个REFCURSOR:
create function test_cursor
return sys_refcursor
is
c_result sys_refcursor;
begin
open c_result for
select * from dual;
return c_result;
end;
更新:如果需要从SQL调用此函数,请使用建议使用的表函数,例如@Tony Andrews。
这是如何构建一个函数,该函数返回可以像表一样查询的结果集:
SQL> create type emp_obj is object (empno number, ename varchar2(10));
2 /
Type created.
SQL> create type emp_tab is table of emp_obj;
2 /
Type created.
SQL> create or replace function all_emps return emp_tab
2 is
3 l_emp_tab emp_tab := emp_tab();
4 n integer := 0;
5 begin
6 for r in (select empno, ename from emp)
7 loop
8 l_emp_tab.extend;
9 n := n + 1;
10 l_emp_tab(n) := emp_obj(r.empno, r.ename);
11 end loop;
12 return l_emp_tab;
13 end;
14 /
Function created.
SQL> select * from table (all_emps);
EMPNO ENAME
---------- ----------
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7902 FORD
7934 MILLER
如果要在普通SQL中使用它,我将让存储过程使用结果行填充表或者临时表(或者使用@Tony Andrews方法)。
如果要使用@Thilo的解决方案,则必须使用PL / SQL循环游标。
这是一个示例:(我使用了过程而不是函数,就像@Thilo一样)
create or replace procedure myprocedure(retval in out sys_refcursor) is
begin
open retval for
select TABLE_NAME from user_tables;
end myprocedure;
declare
myrefcur sys_refcursor;
tablename user_tables.TABLE_NAME%type;
begin
myprocedure(myrefcur);
loop
fetch myrefcur into tablename;
exit when myrefcur%notfound;
dbms_output.put_line(tablename);
end loop;
close myrefcur;
end;

