An example of using SYS_REFCURSOR for cursor variable processing : REFCURSOR « Cursor « Oracle PL/SQL Tutorial






SQL> create table product(
  2     product_id number(4)     not null,
  3     product_description varchar2(20) not null
  4  );

Table created.

SQL>
SQL> insert into product values (1,'Java');

1 row created.

SQL> insert into product values (2,'Oracle');

1 row created.

SQL> insert into product values (3,'C#');

1 row created.

SQL> insert into product values (4,'Javascript');

1 row created.

SQL> insert into product values (5,'Python');

1 row created.

SQL>
SQL>
SQL> DECLARE
  2    refCursorValue SYS_REFCURSOR;
  3    myRecord product%ROWTYPE;
  4  BEGIN
  5    OPEN refCursorValue FOR SELECT *from product;
  6
  7    LOOP
  8      FETCH refCursorValue INTO myRecord;
  9      EXIT WHEN refCursorValue%NOTFOUND;
 10      dbms_output.put_line(to_char(myRecord.product_id)||' '||
 11      rpad(myRecord.product_description,20,' '));
 12    END LOOP;
 13    CLOSE refCursorValue;
 14  END;
 15  /
1 Java
2 Oracle
3 C#
4 Javascript
5 Python

PL/SQL procedure successfully completed.

SQL>
SQL> drop table product;

Table dropped.








25.13.REFCURSOR
25.13.1.Define refcursor variable
25.13.2.Output value in a refcursor
25.13.3.The use of REF CURSOR
25.13.4.An example of using SYS_REFCURSOR for cursor variable processing
25.13.5.Cursor expressions as arguments to functions called from SQL
25.13.6.Subprograms returning resultsets by using SYS_REFCURSOR
25.13.7.Row type Reference cursor
25.13.8.Open SYS_REFCURSOR for select ... from
25.13.9.SYS_REFCURSOR type parameter