PL/SQL print out ref cursor returned by a stored procedure

后端 未结 3 1321
谎友^
谎友^ 2020-12-01 06:27

How can I fetch from a ref cursor that is returned from a stored procedure (OUT variable) and print the resulting rows to STDOUT in SQL*PLUS?

ORACLE stored procedure

相关标签:
3条回答
  • 2020-12-01 06:53

    You can use a bind variable at the SQLPlus level to do this. Of course you have little control over the formatting of the output.

    VAR x REFCURSOR;
    EXEC GetGrantListByPI(args, :x);
    PRINT x;
    
    0 讨论(0)
  • 2020-12-01 07:01

    If you want to print all the columns in your select clause you can go with the autoprint command.

    CREATE OR REPLACE PROCEDURE sps_detail_dtest(v_refcur OUT sys_refcursor)
    AS
    BEGIN
      OPEN v_refcur FOR 'select * from dummy_table';
    END;
    
    SET autoprint on;
    
    --calling the procedure
    VAR vcur refcursor;
    DECLARE 
    BEGIN
      sps_detail_dtest(vrefcur=>:vcur);
    END;
    

    Hope this gives you an alternate solution

    0 讨论(0)
  • 2020-12-01 07:02

    Note: This code is untested

    Define a record for your refCursor return type, call it rec. For example:

    TYPE MyRec IS RECORD (col1 VARCHAR2(10), col2 VARCHAR2(20), ...);  --define the record
    rec MyRec;        -- instantiate the record
    

    Once you have the refcursor returned from your procedure, you can add the following code where your comments are now:

    LOOP
      FETCH refCursor INTO rec;
      EXIT WHEN refCursor%NOTFOUND;
      dbms_output.put_line(rec.col1||','||rec.col2||','||...);
    END LOOP;
    
    0 讨论(0)
提交回复
热议问题