Oracle/REF CURSOR 02
REF CURSOR 02
Soul-Learner
2008. 7. 5. 22:59
FROM: http://www.psoug.org/reference/ref_cursors.html
Strongly Typed | ||||
Note: A REF CURSOR that specifies a specific return type. | ||||
Package Header |
CREATE OR REPLACE PACKAGE strongly_typed IS TYPE return_cur IS REF CURSOR RETURN all_tables%ROWTYPE;
END strongly_typed; / | |||
Package Body |
CREATE OR REPLACE PACKAGE BODY strongly_typed IS
/ | |||
To Run The Demo | set serveroutput on exec strongly_typed.parent(1) exec strongly_typed.parent(8) | |||
Weakly Typed | ||||
Note: A REF CURSOR that does not specify the return type such as SYS_REFCURSOR. | ||||
Child Procedure |
CREATE OR REPLACE PROCEDURE child ( p_NumRecs IN PLS_INTEGER, p_return_cur OUT SYS_REFCURSOR) IS BEGIN OPEN p_return_cur FOR 'SELECT * FROM all_tables WHERE rownum <= ' || p_NumRecs ; END child; / | |||
Parent Procedure |
CREATE OR REPLACE PROCEDURE parent (pNumRecs VARCHAR2) IS p_retcur SYS_REFCURSOR; at_rec all_tables%ROWTYPE; BEGIN child(pNumRecs, p_retcur); FOR i IN 1 .. pNumRecs LOOP FETCH p_retcur INTO at_rec; dbms_output.put_line(at_rec.table_name || ' - ' || at_rec.tablespace_name || ' - ' || TO_CHAR(at_rec.initial_extent) || ' - ' || TO_CHAR(at_rec.next_extent)); END LOOP; END parent; / | |||
To Run The Demo | set serveroutput on exec parent(1) exec parent(17) | |||
Passing Ref Cursors | ||||
Ref Cursor Passing Demo |
CREATE TABLE employees ( empid NUMBER(5), empname VARCHAR2(30)); INSERT INTO employees (empid, empname) VALUES (1, 'Dan Morgan'); INSERT INTO employees (empid, empname) VALUES (2, 'Jack Cline'); INSERT INTO employees (empid, empname) VALUES (3, 'Caleb Small'); COMMIT;
set serveroutput on |