Home arrow Oracle arrow Page 3 - Working with REF CURSOR in PL/SQL

Working with more than one query with the same REF CURSOR - Oracle

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.

TABLE OF CONTENTS:
  1. Working with REF CURSOR in PL/SQL
  2. Working with RECORD and REF CURSOR
  3. Working with more than one query with the same REF CURSOR
  4. Dealing with REF CURSOR in the sub-programs of a PL/SQL block
By: Jagadish Chatarji
Rating: starstarstarstarstar / 448
February 12, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More Oracle Articles          >>> More By Jagadish Chatarji
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

ORACLE ARTICLES

- Oracle Java Security Woes Continue
- Oracle's New IaaS Cloud Option: There's a Ca...
- Oracle Acquires Eloqua to Boost Cloud Presen...
- Choosing Innovation: Oracle Survey Insights
- Oracle Fixes Privilege Escalation Bug
- Oracle`s Communications Service Availability...
- Oracle Releases Exalytics, Taleo Plans
- Oracle Releases Communications Network Integ...
- Oracle Releases Communications Data Model 11...
- Oracle Releases PeopleSoft PeopleTools 8.52
- Oracle Integrates Cloudera Apache Distro, My...
- Oracle Releases MySQL 5.5.18
- Oracle Announces NoSQL Database Availability
- Sorting Database Columns With the SELECT Sta...
- Retrieving Table Data with the LIKE Operator

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: