Discussion in 'SQL PL/SQL' started by suresh88, Mar 1, 2014.
Can anyone explain what is the use of "Ref Cursor" in pl sql
The REF CURSOR is a data type in the Oracle PL/SQL language. It represents a cursor or a result set in Oracle Database.
A REF Cursor is a DATATYPE that holds a CURSOR value in the same way that a VARCHAR2 variable will hold a string value.
A REF Cursor can be opened on the server and passed to the client as a unit rather than fetching one row at a time. One can use a Ref Cursor as target of an assignment, and it can be passed as parameter to other program units. Ref Cursors are opened with an OPEN FOR statement. In most other ways they behave similar to normal cursors.
see link :
sample example :
--Create a function that opens a cursor and returns a reference to it:
CREATE OR REPLACE FUNCTION f
OPEN c FOR SELECT * FROM dual;
--Call the above function and fetch all rows from the cursor it returns:
SET serveroutput ON
DECLARE c SYS_REFCURSOR;
BEGIN c := f();
-- Get ref cursor from function
LOOP FETCH c INTO v;
EXIT WHEN c%NOTFOUND;
dbms_output.put_line('Value from cursor: '||v);
END LOOP; END;