1

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

  • Suggest you avoid using WHEN OTHERS without RAISE which is swallowing the exception. The query string literal you using is too long for the VARCHAR2 (50) you are assigning it to. Note also that dynamic SQL statements should not have trailing semicolons. – Padders Feb 18 '21 at 17:03
  • I will take @Padders suggestion a step further. WHEN OTHERS not followed by a RAISE in virtually always a **bug** especially when not logged. In this case it is the exact reason you got just the headers. What it says is "If I get and run time error, ignore it and do not tell me about it." That is exactly what your function did. Get rid of it and you would the get error messages for what Padders has pointed out. – Belayer Feb 20 '21 at 19:49

0 Answers0