Ref Cursort

2
Introduction to REF CURSOR A REF CURSOR is basically a dat a type. A variable created based on such a data type is generally called a cursor variable. A cursor variable can be associated with different queries at run-time. The primary advantage of using cursor variables is their capability to pass result sets between sub programs (like stored procedures, functions, packages etc.). Let us start with a small sub-program as follows: declare type r_cursor is REF CURSOR; c_emp r_cursor; en emp.ename%type; begin open c_emp for select ename from emp; loop fetch c_emp into en; exit when c_emp%notfound; dbms_output.put_line(en); end loop; close c_emp; end; Let me explain step by step. The following is the first statement y ou need to understand: type r_cursor is REF CURSOR;  The above statement simply defines a new data type called "r_cursor," which is of the type REF CURSOR. We declare a cursor variable named "c_emp" based on the type "r_cursor" as follows: c_emp r_cursor;  Every cursor variable must be opened with an associated SELECT statement as follows: open c_emp for select ename from emp;  To retrieve each row of information from the cursor, I used a loop together with a FETCH statement as follows: loop fetch c_emp into en; exit when c_emp%notfound; dbms_output.put_line(en); end loop; I finally closed the cursor using the following statement:

Transcript of Ref Cursort

8/2/2019 Ref Cursort

http://slidepdf.com/reader/full/ref-cursort 1/2

Introduction to REF CURSOR 

A REF CURSOR is basically a data type. A variable created based on such a data type is

generally called a cursor variable. A cursor variable can be associated with different queries at

run-time. The primary advantage of using cursor variables is their capability to pass result sets

between sub programs (like stored procedures, functions, packages etc.).

Let us start with a small sub-program as follows:

declaretype r_cursor is REF CURSOR;c_emp r_cursor;en emp.ename%type;

beginopen c_emp for select ename from emp;loop

fetch c_emp into en;exit when c_emp%notfound;dbms_output.put_line(en);

end loop;close c_emp;

end; 

Let me explain step by step. The following is the first statement you need to understand:

type r_cursor is REF CURSOR; 

The above statement simply defines a new data type called "r_cursor," which is of the type REF

CURSOR. We declare a cursor variable named "c_emp" based on the type "r_cursor" as

follows:

c_emp r_cursor; 

Every cursor variable must be opened with an associated SELECT statement as follows:

open c_emp for select ename from emp; 

To retrieve each row of information from the cursor, I used a loop together with a FETCH

statement as follows:

loopfetch c_emp into en;exit when c_emp%notfound;dbms_output.put_line(en);

end loop; 

I finally closed the cursor using the following statement:

8/2/2019 Ref Cursort

http://slidepdf.com/reader/full/ref-cursort 2/2

close c_emp; 

%ROWTYPE with REF CURSOR 

In the previous section, I retrieved only one column (ename) of information using REF

CURSOR. Now I would like to retrieve more than one column (or entire row) of informationusing the same. Let us consider the following example:

declaretype r_cursor is REF CURSOR;c_emp r_cursor;er emp%rowtype;

beginopen c_emp for select * from emp;loop

fetch c_emp into er;exit when c_emp%notfound;dbms_output.put_line(er.ename || ' - ' || er.sal);

end loop;close c_emp;

end; 

In the above example, the only crucial declaration is the following:

er emp%rowtype; 

The above declares a variable named "er," which can hold an entire row from the "emp"

table. To retrieve the values (of each column) from that variable, we use the dot notation as

follows:

dbms_output.put_line(er.ename || ' - ' || er.sal); 

Let us consider that a table contains forty columns and I would like to retrieve fifteen

columns. In such scenarios, it is a bad idea to retrieve all forty columns of information. At the

same time, declaring and working with fifteen variables would be bit clumsy. The next section

will explain how to solve such issues.

Read more at http://www.devshed.com/c/a/Oracle/Working-with-REF-CURSOR-in-PL-

SQL/#jFmbCRGhrC6r3wfM.99