Working with REF CURSOR in PL/SQL (
Page 1 of 4 )
This article introduces you to the REF CURSOR in Oracle PL/SQL. I've included numerous examples to help you understand how to work with REF CURSOR.All the examples in this series have been tested only with Oracle 10g (V10.2). I didn't really test any of the examples in any of the previous versions. If you have any problems during the execution of these examples, please post in the discussion area.
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:
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 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:
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:
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 information using the same. Let us consider the following example:
declare
type r_cursor is REF CURSOR;
c_emp r_cursor;
er emp%rowtype;
begin
open 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.
| | Discuss Working with REF CURSOR in PL/SQL | | | | | | | This is a simple introduction to REF CURSOR in PL/SQL. Hope you will enjoy it. If... | | | | | | dear sir,
this article is very use full for the beginner like me.
Thanks a... | | | | | | It is very good article for the starters in PL/SQL | | | | | | thanks!
this was a great article for me and gave me new insights. | | | | | | I would like to know how to handle data from a ref cursor to a record type when i... | | | | | | I liked they way,all your topics are put in.This one was also very easy to... | | | | | | This is really to good explanation with examples. I was able to understand it in a... | | | | | | very good one | | | | | | great way of explaining the concept | | | | | | Its really a good way of explaining ina simple way..
Thanks
Jeg | | | | | | Thanks Jag for the enlightening article on Reference cursors...it was a lot more... | | | | | | Thanks allot for providing us a very good description on Ref Cursors. I found this... | | | | | | Excellent steps of understanding REFCURSOR concept with simple example... | | | | | | This is an example of telling a complex thing in a SIMPLE way. Hats off to you.... | | | | | | Thanks a lot for the post. This is good article and keep posting things with similar... | | | | | | The Content is Very Useful for my Knowledge and more than This is Really a Good Work... | | | | | | Itīs only confused to use ref cursor in page 1-3.
The only reason to use ref cursor... | | | | | | has anybody tried using ref cursor in 10g. Because I have a proc which passes ref... | | | | | | The examples posted here are really helpful and can be easily comprehended.
If you... | | | | | | this topic civering ref cursors was very good. especially for beginners!!! | | | | | | its great to peek into it for 2 minutes and to get in hand all the imp points reg... | | | | | | Excellent article! Thanks. | | | | | | Really helpful. In less than 15 min I could get everything I ever wanted to know... | | | | | | its great to peek into it for 2 minutes and to get in hand all the imp points reg... | | | | | | Dear Sir,
Realy its very easy to study and understand...Thanx for... | | | | | | This is really good presentation for understanding the ref_cursors topic .
this is... | | | | | | Very nice artical. It gives very clear idea of ref cursor. Thank you for this. | | | | | | very helpfull | | | | | | You are amazing. If all programming books were written the way you do, learning to... | | | | | | Dear Jagadish,
I wish other authors 'learned' the flow and style that you use.
It... | | | | | | Hi sir,
Basically before i read this article i have a little idea on REF... | | | | | | How do you use a ref cursor,in PL/SQL, when you don't know the number of columns... | | | | | | It seems like you could do it in PL/SQL, but you can't before version 11g. Variable... | | | | | | Good explanation, however just like to point out one flaw with the... | | | | | | Hi Sir
This article is very good and very usefull to me, the way of... | | | | | | >>> Post your comment now! | | | | | |
|
 |