Working with REF CURSOR in PL/SQL - Working with more than one query with the same REF CURSOR (
Page 3 of 4 )
As defined earlier, a REF CURSOR can be associated with more than one SELECT statement at run-time. Before associating a new SELECT statement, we need to close the CURSOR. Let us have an example as follows:
declare
type r_cursor is REF CURSOR;
c_emp r_cursor;
type rec_emp is record
(
name varchar2(20),
sal number(6)
);
er rec_emp;
begin
open c_emp for select ename,sal from emp where deptno = 10;
dbms_output.put_line('Department: 10');
dbms_output.put_line('--------------');
loop
fetch c_emp into er;
exit when c_emp%notfound;
dbms_output.put_line(er.name || ' - ' || er.sal);
end loop;
close c_emp;
open c_emp for select ename,sal from emp where deptno = 20;
dbms_output.put_line('Department: 20');
dbms_output.put_line('--------------');
loop
fetch c_emp into er;
exit when c_emp%notfound;
dbms_output.put_line(er.name || ' - ' || er.sal);
end loop;
close c_emp;
end;
In the above program, the skeleton looks like the following:
declare
.
.
Begin
.
.
open c_emp for select ename,sal from emp where deptno = 10;
.
.
fetch c_emp into er;
.
.
close c_emp;
.
.
open c_emp for select ename,sal from emp where deptno = 20;
.
.
fetch c_emp into er;
.
.
close c_emp;
.
.
end;
From the above skeleton, you can easily understand that every CURSOR is opened, used and closed before opening the same with the next SELECT statement.
Working with REF CURSOR inside loops
Sometimes, it may be necessary for us to work with REF CURSOR within loops. Let us consider the following example:
declare
type r_cursor is REF CURSOR;
c_emp r_cursor;
type rec_emp is record
(
name varchar2(20),
sal number(6)
);
er rec_emp;
begin
for i in (select deptno,dname from dept)
loop
open c_emp for select ename,sal from emp where deptno = i.deptno;
dbms_output.put_line(i.dname);
dbms_output.put_line('--------------');
loop
fetch c_emp into er;
exit when c_emp%notfound;
dbms_output.put_line(er.name || ' - ' || er.sal);
end loop;
close c_emp;
end loop;
end;
As you can observe from the above program, I implemented a FOR loop as follows:
for i in (select deptno,dname from dept)
loop
.
.
end loop;
The above loop iterates continuously for each row of the "dept" table. The details of each row in "dept" (like deptno, dname etc.) will be available in the variable "i." Using that variable (as part of the SELECT statement), I am working with REF CURSOR as follows:
open c_emp for select ename,sal from emp where deptno = i.deptno;
The rest of the program is quite commonplace.
| | 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... | | | | | | explanation of this topic is awesome.....and I enjoyed working with this ref... | | | | | | Its really a best example for understanding a REF Cursor in PL/SQL.I am sure if any... | | | | | | Excellent article. The author can read minds of his readers. | | | | | | This is v.good example for the beginners | | | | | | Hi jagadish,
Very good explanation.It gave a clear understanding of... | | | | | | gud job:)) | | | | | | This article is very useful for me as a beginner.Thank u very much for your good... | | | | | | Excellent explanation!!! Whenever I go through any article in DevShed regarding... | | | | | | Thank you very much for this sort of step by step deliberation. I hope it will help... | | | | | | dear sir,
this article is very use full for me.
Thanks a lot.
from,... | | | | | | >>> Post your comment now! | | | | | |
|
 |