如何从存储过程返回多行? (Oracle PL / SQL)
我想用一个参数创建一个存储过程,该存储过程将根据参数返回不同的记录集。 这是怎么做的? 我可以从普通SQL中调用它吗?
5个解决方案
65 votes
这是如何构建一个函数,该函数返回可以像表一样查询的结果集:
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
Tony Andrews answered -06-30T04:27:23Z
22 votes
我认为您想返回一个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。
Thilo answered -06-30T04:27:48Z
8 votes
您可以使用Oracle流水线函数
基本上,当您希望将PLSQL(或Java或C)例程作为“源”时 数据-而不是表-您将使用流水线函数。
简单示例-生成一些随机数据
如何根据输入参数创建N个唯一的随机数?
create type array
as table of number;
create function gen_numbers(n in number default null)
return array
PIPELINED
as
begin
for i in 1 .. nvl(n,999999999)
loop
pipe row(i);
end loop;
return;
end;
假设我们需要三行内容。 现在,我们可以通过以下两种方式之一进行操作:
select * from TABLE(gen_numbers(3));
COLUMN_VALUE
1
2
3
要么
select * from TABLE(gen_numbers)
where rownum <= 3;
COLUMN_VALUE
1
2
3
管道功能1管道功能2
Mohsen Heydari answered -06-30T04:28:45Z
3 votes
如果要在普通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;
John Smithers answered -06-30T04:29:10Z
1 votes
create procedure (p_cur out sys_refcursor) as begin open p_cur for select * from end;
S. Mayol answered -06-30T04:29:26Z