Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

PROCEDURE GetGrantListByPI(p_firstname IN VARCHAR2, p_lastname IN VARCHAR2, p_orderby IN VARCHAR2, p_cursor OUT grantcur); 

PL/SQL:

SET SERVEROUTPUT ON;  DECLARE   TYPE r_cursor IS REF CURSOR;   refCursor r_cursor;    CURSOR grantCursor IS     SELECT last_name, first_name     FROM ten_year_pis     WHERE year_added = 2010;    last_name VARCHAR2(100);   first_name VARCHAR2(100);  BEGIN   OPEN grantCursor;   FETCH grantCursor INTO last_name, first_name;    WHILE grantCursor%FOUND LOOP     PMAWEB_PKG.GetGrantListByPI(last_name, first_name, 'last_name', refCursor);      --HOW DO I LOOP THROUGH THE RETURNED REF CURSOR (refCursor)     --AND PRINT THE RESULTING ROWS TO STDOUT?      FETCH grantCursor into last_name, first_name;   END LOOP;   CLOSE grantCursor; END; / 
like image 549
elpisu Avatar asked Apr 28 '11 16:04

elpisu


People also ask

Is ref cursor return PL SQL?

Introduction to REF CURSORs Using REF CURSOR s is one of the most powerful, flexible, and scalable ways to return query results from an Oracle Database to a client application. A REF CURSOR is a PL/SQL data type whose value is the memory address of a query work area on the database.

How do I return a ref cursor in Oracle?

CREATE OR REPLACE FUNCTION TEST_CUR RETURN SYS_REFCURSOR AS VAR_REF SYS_REFCURSOR; BEGIN OPEN VAR_REF FOR SELECT * FROM DUAL; RETURN VAR_REF; END; Here you don't need to open the cursor, it is already opened. Where should we close the cursor? In this case, Oracle will close the cursor as soon as it is out of scope.


2 Answers

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; 
like image 174
DCookie Avatar answered Sep 23 '22 10:09

DCookie


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; 
like image 30
Dave Costa Avatar answered Sep 24 '22 10:09

Dave Costa