View results of refcursor out parameter

A lot of times, pl/sql developers may need to check the results of refcursor cursor 
which was passed in a stored procedure as a out parameter for the debugging purpose. 

Lets say we have following oracle stored procedure:

CREATE OR REPLACE PROCEDURE MYPROC (CR OUT SYS_REFCURSOR)
IS
BEGIN
 OPEN CR FOR 
  SELECT EMPNO, ENAME, DNAME, SAL 
  FROM SCOTT.EMP E, SCOTT.DEPT D 
  WHERE E.DEPTNO = D.DEPTNO ORDER BY SAL;
END;
/


Solution 1: The results of refcursor can be easily viewed by making an anonymous 
block as following

SQL> SET SERVEROUTPUT ON

SQL> DECLARE
  2     C SYS_REFCURSOR;
  3     ENAME VARCHAR(50);
  4     EMPNO NUMBER;
  5     DNAME VARCHAR2(50);
  6     SAL NUMBER;
  7  BEGIN
  8     MYPROC(C);
  9     LOOP
 10       FETCH C INTO EMPNO, ENAME, DNAME, SAL;
 11       EXIT WHEN C%NOTFOUND;
 12       DBMS_OUTPUT.PUT_LINE(EMPNO || ' ' || ENAME || ' ' || DNAME || ' ' || SAL);
 13     END LOOP;
 14  END;
 15  /
 
7934 MILLER OPERATIONS 0
7369 SMITH RESEARCH 800
7900 JAMES SALES 950
7876 ADAMS RESEARCH 1100
7521 WARD SALES 1250
7654 MARTIN SALES 1250
7844 TURNER SALES 1500
7499 ALLEN SALES 1600
7782 CLARK ACCOUNTING 2450
7698 BLAKE SALES 2850
7566 JONES RESEARCH 2975
7788 SCOTT RESEARCH 3000
7902 FORD RESEARCH 3000
7839 KING ACCOUNTING 5000

PL/SQL procedure successfully completed.


Solution 2: But the nice and simple way to get the output of refcursor returned 
by an oracle stored procedure is as following:

SQL> VAR C REFCURSOR

SQL> EXECUTE MYPROC(:C);

PL/SQL procedure successfully completed.

SQL> PRINT C

     EMPNO ENAME      DNAME                 SAL
---------- ---------- -------------- ----------
      7934 MILLER     OPERATIONS              0
      7369 SMITH      RESEARCH              800
      7900 JAMES      SALES                 950
      7876 ADAMS      RESEARCH             1100
      7521 WARD       SALES                1250
      7654 MARTIN     SALES                1250
      7844 TURNER     SALES                1500
      7499 ALLEN      SALES                1600
      7782 CLARK      ACCOUNTING           2450
      7698 BLAKE      SALES                2850
      7566 JONES      RESEARCH             2975
      7788 SCOTT      RESEARCH             3000
      7902 FORD       RESEARCH             3000
      7839 KING       ACCOUNTING           5000

14 rows selected.


Related Links
- Cursor with Parameters in Oracle
- Ref Cursor: Strongly Typed VS Weakly Typed
- Difference Between Cursor And Ref cursor
- ORA-01422: exact fetch returns more than requested number of rows
Get Results from REFCURSOR with unknown number of columns in PLSQL

21 comments:

  1. very helpfull Blog... keep writing.
    Please write about Oracle indexes. and performance tuning

    Regard's
    Nirmesh

    ReplyDelete
    Replies
    1. Please check following link:
      http://nimishgarg.blogspot.in/2012/10/query-optimization-tips-for-oracle.html

      Delete
  2. This is really simple. Thanx :)

    ReplyDelete
  3. Hi,

    What is Function Oveloading in Oracle ..Plz explain the steps....

    ReplyDelete
    Replies
    1. http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/08_subs.htm#12353

      Delete
  4. Nice one :-) what is the difference between procedure and stored procedure?

    ReplyDelete
  5. nice one :-) can you please explain in detail What is Ref Cursor with example? and advantage of using ref Cursor.

    ReplyDelete
    Replies
    1. http://docs.oracle.com/cd/E17781_01/appdev.112/e18751/procedures_plsql.htm

      Delete
  6. excellent stuff....
    could you just explain me how to do this with out using ref cursor.
    i just want to know pass the result set into front end using collections(index by tables) same as ref cursor.

    ReplyDelete
    Replies
    1. https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1058232381458

      Delete
    2. thank you for posting...and i got some knowledge in that document.
      but could you tell me..... collection as a parameter in procedure in the place of sys_refcursor.

      Delete
    3. If you want to return record set to your front-end application, my suggestion is to use pipeline functions or ref cursors.

      Delete
  7. Yassen:
    Very good knodlege about refcursor.
    Thanks.

    ReplyDelete
  8. Great articles about cursors and refcursor!
    One question .. how can i enable that PRINT command? My TOAD does not recognize it (neither SQL Plus). It is not SQL , nor PL/SQL command, how can one use it?

    Thanks in advance!

    ReplyDelete
  9. Very nice blog , very very thanks.
    Please keep continue sharing..........

    ReplyDelete
  10. Nice post, Nimish. Now would you care to tackle the question of "What happens if I do not know as I am writing my code how many columns I am fetching?" :-)

    ReplyDelete
    Replies
    1. I believe you are asking about refcursor only so with my limited knowledge I can remember only following at this moment
      - in sqlplus print statement is there
      - in sql developer we have output variable section
      - also we can directly print refcursor if it is being returned from function using select function from dual;
      - and from Oracle 12c we have dbms_sql.return_result

      please share the links about other techniques, i would love to learn.

      Delete
  11. nd also we can use DBMS_SQL.TO_CURSOR_NUMBER to convert refcursor to dbms_sql and then print it...

    ReplyDelete
  12. Post writing is also a excitement, if you know then you can write otherwise it is complicated to write.

    ReplyDelete