SQL>
SQL>
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>
SQL> CREATE OR REPLACE FUNCTION getAllHierarchies
2 RETURN SYS_REFCURSOR
3 IS
4 refCursorValue SYS_REFCURSOR;
5 BEGIN
6 OPEN refCursorValue FOR SELECT * FROM product;
7
8 RETURN (refCursorValue);
9
10 EXCEPTION WHEN OTHERS THEN
11
12 RAISE_APPLICATION_ERROR(-20002,SQLERRM);
13
14 END;
15 /
Function created.
SQL>
SQL> DECLARE
2 refCursorValue SYS_REFCURSOR;
3 myRecord product%ROWTYPE;
4 BEGIN
5 refCursorValue :=getAllHierarchies;
6
7 LOOP
8
9 FETCH refCursorValue INTO myRecord;
10
11 EXIT WHEN refCursorValue%NOTFOUND;
12
13 dbms_output.put_line(TO_CHAR(myRecord.product_id)||' '||myRecord.product_description);
14
15 END LOOP;
16
17 EXCEPTION WHEN OTHERS THEN
18
19 dbms_output.put_line(TO_CHAR(SQLCODE)||' '||SQLERRM);
20
21 END;
22 /
1 Java
2 Oracle
3 C#
4 Javascript
5 Python
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL> drop table product;
Table dropped.
SQL>