Open SYS_REFCURSOR for select ... from : sys_refcursor « Cursor « Oracle PL / SQL






Open SYS_REFCURSOR for select ... from

 

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

Procedure created.

SQL>
SQL> COL fname FORMAT A12
SQL> VARIABLE x REFCURSOR
SQL> EXEC emp_sel(:x)

PL/SQL procedure successfully completed.

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>
SQL> drop table emp;

Table dropped.

   
  








Related examples in the same category

1.sys_refcursor type variable
2.SYS_REFCURSOR type parameter
3.return sys_refcursor from a function