如何用普通游标的值打开 sys_refcursor?

How to open a sys_refcursor with values from a normal cursor?

我可以用普通游标的值打开 sys_refcursor 吗?

create or replace procedure test(C1 out sys_refcursor)
  Lv_c1 as
    Select * from table;
Begin
  Open C1 for select * from lv_c1;
End;

不,你不能。 “普通”游标是一个 PL/SQL 变量,因此不能在 SQL 查询中使用。

但是可以为游标变量的结果集打开一个游标:

create or replace package pack as 
    cursor cur is 
        select rownum attr_1 from dual connect by level<=3;
    type rset is table of cur%rowtype;     
    procedure getCursor (rc out sys_refcursor);
end;
/
create or replace package body pack as 
    procedure getCursor (rc out sys_refcursor) is
        rs rset; 
    begin
        open cur;
        fetch cur bulk collect into rs;
        close cur;    
        open rc for select * from table (rs);
    end;
end;
/

执行和结果:

var rc refcursor
exec pack.getCursor (:rc)  

ATTR_1
--------
row1
row2
row3