2

Can I open a sys_refcursor with value from a normal cursor?

create or replace procedure test(C1 out sys_refcursor)
  Lv_c1 as
    Select * from table;
Begin
  Open C1 for select * from lv_c1;
End;
0xdb
  • 3,539
  • 1
  • 21
  • 37
TBose
  • 115
  • 2
  • 10

1 Answers1

5

No, you cannot. "Normal" cursor is a PL/SQL variable, so it cannot be used in SQL query.

But it's possible to open a cursor for a result set of a cursor variable:

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;
/

Execution and the result:

var rc refcursor
exec pack.getCursor (:rc)  

ATTR_1
--------
row1
row2
row3
0xdb
  • 3,539
  • 1
  • 21
  • 37
  • The cursor I have has 15 columns and when I get the result it says ATTR_1 to ATTR_15 instead of the original column names in the cursor. Is there a way to retain the column name rather than aliasing each column again. – Rajiv A Mar 31 '21 at 22:32
  • It's [known issue](https://asktom.oracle.com/pls/apex/asktom.search?tag=oracle-19c-database-issue-with-table-types-and-pipelining#followup-) in 18c, 19c. As workaround you must use either a table or a view, something like `my_view%rowtype;`. – 0xdb Mar 31 '21 at 22:44
  • @RajA [QA on so](https://stackoverflow.com/q/60225275). – 0xdb Mar 31 '21 at 22:50
  • I am getting this error with this approach: ```SQL Error [21700] [72000]: ORA-21700: object does not exist or is marked for delete```. Any idea ? – Jeterson Miranda Gomes Sep 01 '22 at 00:44
  • @JetersonMirandaGomes please try [to reproduce](https://dbfiddle.uk/?rdbms=oracle_21&fiddle=9077b0808efa4d2691ecd0cca8b3d584) your issue. – 0xdb Sep 01 '22 at 13:34
  • 1
    I check this link, I see that version of Oracle is 21c, and my environment is using oracle 11g. The problem is a version. Thank you. – Jeterson Miranda Gomes Sep 02 '22 at 18:54