Ref Cursor is a variable of type cursor. It is a pointer to work area of the result set obtained by executing a select statement.
Steps to create and use the ref cursor.
1. Define the variable of type REF CURSOR
2. Pass that variable to a procedure or function which actually executest
the sql statement for the cursor and returns the cursor variable as OUT
variable.
You must pass the ref cursor variable type as IN OUT argument to the
stored procedure or function.
Creating Package Spec
Create or replace package customer_info as
--This is the way you define a variable
of type REF CURSOR
TYPE cust_data_cursor IS REF CURSOR;
--Now we can send this variable to a
procedure which populates the result set by using a select statement.
PROCEDURE get_cust_bystate ( var_state_code in varchar,
var_ref_cursor in out cust_data_cursor );
End customer_info;
Creating Package Body
Create or replace package body customer_info as
PROCEDURE get_cust_by_state ( var_state_code in varchar, var_ref_cursor
in out cust_data_cursor ) is
begin
Open cust_data_cursor FOR
Select * from customer where state_code = var_state_code;
Exception
When others then
Raise_Application_Error
( -20001, 'Error while executing get_cust_by_state in customer_info package
');
End get_cust_by_state;
End customer_info;