Oracle pl/sql assign variable holding sql text to sys_refcursor and loop through the records

201 Views Asked by At

I have below code where function ReturnSysRef returns SYS_REFCURSOR, this function holds the SQL text in a variable and then opens the SYS_REFCURSOR on that variable and returns it. Function MyFunc is a pipelined function which calls ReturnSysRef to loop through the records and then returns record defined explicitly. The issue is when I try to call pipelined function I only get column headings back but not data.

CREATE OR REPLACE PACKAGE MyPackage IS
   
    type MyRecords is record
    (
        Col1   varchar2, 
        Col2   varchar2, 
        Col3   varchar2
    );    
    
    type MyList is table of MyRecord;
    function MyFunc return MyList pipelined;
    function ReturnSysRef return SYS_REFCURSOR;
END;
/
CREATE OR REPLACE package body MyPackage is
function MyFunc return MyList pipelined is
    
       l_row MyRecord;
       l_ref SYS_REFCURSOR;
    begin
        
        l_ref:=ReturnSysRef;
          LOOP 
            FETCH l_ref
            INTO  l_row.col1, l_row.col2, l_row.col3;;
            EXIT WHEN l_ref%NOTFOUND;
            pipe row(l_row);
          END LOOP;
          CLOSE l_ref;
    
    <<Proc_Exit>>
        return;
    Exception
        When others then
            return;
    end MyFunc;
    
    
function ReturnSysRef return SYS_REFCURSOR is
    l_sqlqry varchar2(50);
    l_ref SYS_REFCURSOR;
    begin
    
        l_sqlqry:='select ''val1'' as col1, ''val2'' as col2, ''val3'' as col3 from dual;';
        
        open l_ref for
        l_sqlqry;
        
        return l_ref;
    end ReturnSysRef;
end;
/

when I execute below query it does not return any data while I expect it to return "val1, val2, val3".

select * From table(mypackage.Myfunc);

0

There are 0 best solutions below