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.
- 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
very helpfull Blog... keep writing.
ReplyDeletePlease write about Oracle indexes. and performance tuning
Regard's
Nirmesh
Please check following link:
Deletehttp://nimishgarg.blogspot.in/2012/10/query-optimization-tips-for-oracle.html
This is really simple. Thanx :)
ReplyDeleteHi,
ReplyDeleteWhat is Function Oveloading in Oracle ..Plz explain the steps....
http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/08_subs.htm#12353
DeleteNice one :-) what is the difference between procedure and stored procedure?
ReplyDeletethere is no difference
Deletenice one :-) can you please explain in detail What is Ref Cursor with example? and advantage of using ref Cursor.
ReplyDeletehttp://docs.oracle.com/cd/E17781_01/appdev.112/e18751/procedures_plsql.htm
Deleteexcellent stuff....
ReplyDeletecould 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.
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1058232381458
Deletethank you for posting...and i got some knowledge in that document.
Deletebut could you tell me..... collection as a parameter in procedure in the place of sys_refcursor.
If you want to return record set to your front-end application, my suggestion is to use pipeline functions or ref cursors.
Deletethank you very much.
DeleteYassen:
ReplyDeleteVery good knodlege about refcursor.
Thanks.
Great articles about cursors and refcursor!
ReplyDeleteOne 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!
Very nice blog , very very thanks.
ReplyDeletePlease keep continue sharing..........
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?" :-)
ReplyDeleteI believe you are asking about refcursor only so with my limited knowledge I can remember only following at this moment
Delete- 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.
nd also we can use DBMS_SQL.TO_CURSOR_NUMBER to convert refcursor to dbms_sql and then print it...
ReplyDeletePost writing is also a excitement, if you know then you can write otherwise it is complicated to write.
ReplyDelete