如何从存储过程返回多个行?(Oracle PL / SQL)

时间:2022-04-05 22:47:34

I want to create a stored procedure with one argument which will return different sets of records depending on the argument. What is the way to do this? Can I call it from plain SQL?

我想创建一个包含一个参数的存储过程,根据参数返回不同的记录集。怎么做呢?我可以用纯SQL调用它吗?

5 个解决方案

#1


64  

Here is how to build a function that returns a result set that can be queried as if it were a table:

下面是如何构建一个返回结果集的函数,该结果集可以像查询表一样查询:

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

#2


22  

I think you want to return a REFCURSOR:

我想你想要返回一个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;

Update: If you need to call this from SQL, use a table function like @Tony Andrews suggested.

更新:如果需要从SQL调用,请使用@Tony Andrews建议的表函数。

#3


8  

You may use Oracle pipelined functions

您可以使用Oracle管道函数

Basically, when you would like a PLSQL (or java or c) routine to be the «source» of data -- instead of a table -- you would use a pipelined function.

基本上,当您希望PLSQL(或java或c)例程是数据的«source»而不是表时,您将使用流水线函数。

Simple Example - Generating Some Random Data
How could you create N unique random numbers depending on the input argument?

简单的例子——生成一些随机数据你怎么能根据输入参数创建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;

Suppose we needed three rows for something. We can now do that in one of two ways:

假设我们需要三行。我们现在可以通过以下两种方式来做到:

select * from TABLE(gen_numbers(3));

COLUMN_VALUE

COLUMN_VALUE


       1
       2
       3

or

select * from TABLE(gen_numbers)
 where rownum <= 3;

COLUMN_VALUE

COLUMN_VALUE


       1
       2
       3

pipelied Functions1 pipelied Functions2

pipelied Functions1 pipelied Functions2

#4


3  

If you want to use it in plain SQL, I would let the store procedure fill a table or temp table with the resulting rows (or go for @Tony Andrews approach).
If you want to use @Thilo's solution, you have to loop the cursor using PL/SQL. Here an example: (I used a procedure instead of a function, like @Thilo did)

如果您想在纯SQL中使用它,我将让存储过程填充一个表或temp表,并使用结果行(或使用@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;

#5


0  

create procedure <procedure_name>(p_cur out sys_refcursor) as begin open p_cur for select * from <table_name> end;

#1


64  

Here is how to build a function that returns a result set that can be queried as if it were a table:

下面是如何构建一个返回结果集的函数,该结果集可以像查询表一样查询:

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

#2


22  

I think you want to return a REFCURSOR:

我想你想要返回一个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;

Update: If you need to call this from SQL, use a table function like @Tony Andrews suggested.

更新:如果需要从SQL调用,请使用@Tony Andrews建议的表函数。

#3


8  

You may use Oracle pipelined functions

您可以使用Oracle管道函数

Basically, when you would like a PLSQL (or java or c) routine to be the «source» of data -- instead of a table -- you would use a pipelined function.

基本上,当您希望PLSQL(或java或c)例程是数据的«source»而不是表时,您将使用流水线函数。

Simple Example - Generating Some Random Data
How could you create N unique random numbers depending on the input argument?

简单的例子——生成一些随机数据你怎么能根据输入参数创建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;

Suppose we needed three rows for something. We can now do that in one of two ways:

假设我们需要三行。我们现在可以通过以下两种方式来做到:

select * from TABLE(gen_numbers(3));

COLUMN_VALUE

COLUMN_VALUE


       1
       2
       3

or

select * from TABLE(gen_numbers)
 where rownum <= 3;

COLUMN_VALUE

COLUMN_VALUE


       1
       2
       3

pipelied Functions1 pipelied Functions2

pipelied Functions1 pipelied Functions2

#4


3  

If you want to use it in plain SQL, I would let the store procedure fill a table or temp table with the resulting rows (or go for @Tony Andrews approach).
If you want to use @Thilo's solution, you have to loop the cursor using PL/SQL. Here an example: (I used a procedure instead of a function, like @Thilo did)

如果您想在纯SQL中使用它,我将让存储过程填充一个表或temp表,并使用结果行(或使用@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;

#5


0  

create procedure <procedure_name>(p_cur out sys_refcursor) as begin open p_cur for select * from <table_name> end;