Reference cursor : ref cursor « Cursor « Oracle PL/SQL Tutorial






SQL>
SQL> CREATE TABLE emp (
  2    id         NUMBER PRIMARY KEY,
  3    fname VARCHAR2(50),
  4    lname  VARCHAR2(50)
  5  );

Table created.

SQL>
SQL> INSERT INTO emp (id, fname, lname)VALUES (1, 'A', 'B');

1 row created.

SQL> INSERT INTO emp (id, fname, lname)VALUES (2, 'C', 'D');

1 row created.

SQL> INSERT INTO emp (id, fname, lname)VALUES (3, 'Enn', 'F');

1 row created.

SQL> INSERT INTO emp (id, fname, lname)VALUES (4, 'G', 'H');

1 row created.

SQL> INSERT INTO emp (id, fname, lname)VALUES (5, 'G', 'Z');

1 row created.

SQL>
SQL>
SQL> SET SERVEROUTPUT ON ESCAPE OFF
SQL>
SQL> CREATE OR REPLACE PROCEDURE emp_sel (cv_results IN OUT SYS_REFCURSOR)
  2  IS
  3  BEGIN
  4     OPEN cv_results FOR SELECT id, fname, lname FROM emp;
  5  END;
  6  /

Procedure created.

SQL>
SQL> VARIABLE x REFCURSOR
SQL>
SQL> EXEC emp_sel(:x)

PL/SQL procedure successfully completed.

SQL>
SQL> print x

        ID FNAME
---------- --------------------------------------------------
LNAME
--------------------------------------------------
         1 A
B

         2 C
D

         3 Enn
F

         4 G
H

         5 G
Z


5 rows selected.

SQL>
SQL> drop table emp;

Table dropped.

SQL>








25.12.ref cursor
25.12.1.Create and use reference cursor
25.12.2.Creating a REF CURSOR datatype
25.12.3.Timing a ref cursor
25.12.4.REF CURSOR custom type
25.12.5.Reference cursor
25.12.6.Reference value in a cursor by cursor variable