How to return multiple rows from the stored procedure? (Oracle PL/SQL)

前端 未结 5 1987
野的像风
野的像风 2020-11-27 11:19

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 p

5条回答
  •  隐瞒了意图╮
    2020-11-27 11:55

    You may use Oracle pipelined functions

    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.

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

    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


           1
           2
           3
    

    or

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

    COLUMN_VALUE


           1
           2
           3
    

    pipelied Functions1 pipelied Functions2

提交回复
热议问题