Ref Cursor
Ref Cursor
What is a ref cursor and how is the data fetched Let's start with a simple function that opens a ref cursor and passes it back.. (We'll just select some employee details for a specified department) SQL> create or replace function get_dept_emps(p_deptno in number) return sys_refcursor is 2 v_rc sys_refcursor; 3 begin 4 open v_rc for select empno! ename! mgr! sal from emp "#ere deptno $ %deptno using p_deptno; & return v_rc; ' end; ( ) *unction created+ Now, if we look at using this through !L"#lus we first create oursel$es a ref cursor $ariable to accept the results of the function, and then call the function to get the ref cursor back.. (Note% !L"#lus $ariable type of &refcursor& is the e'ui$alent of #L( !L's &sys)refcursor&) SQL> var rc refcursor SQL> e,ec %rc %$ get_dept_emps(-.); /L)SQL procedure successfully completed+ *k, so our $ariable &rc& has our ref cursor. +f we use !L"#lus' print command now we get.. SQL> print rc; 01/23 02410 156 S4L 7777777777 7777777777 7777777777 7777777777 ((82 9L46: (83; 24&. (83; :<25 &... (;34 1<LL06 ((82 -3.. ,rilliant, so our ref cursor returns the rows we wanted from the employee table.
A variable of type REF CURSOR is referred to as a cursor variable. We can define a cursor variable as type SYS_REFCURSOR and retrieve a record set from a procedure or function. SYS_REFCURSOR is a ea!ly typed REF CURSOR type t"at as provided it" #$%S&$ be'innin' it" Oracle (iR). *"e follo in' e+ample is a simple procedure t"at returns a record set from t"e AUTHORS table,
Available as part of Ref_Cursor.sql CREATE OR REPLACE PROCEDURE authors_sel ( cv_results IN OUT SYS_REFCURSOR) IS BEGIN OPEN cv_results FOR SELECT id, first_name, last_name FROM authors; END; Cursor variable can not define in a package. PL SQL collection can not store cursor variable. Database table and views can not store REF cursor columns.
What is reference -ursor. / ref cursor is basically a data type. / $ariable created based on such a data type is generally called a cursor $ariable.
/ -ursor $ariable can be associate with different 'ueries at run time. 0ain ad$antage of using cursor $ariable is their capability to pass result sets between sub programs.