Oracle
  Home arrow Oracle arrow Working with REF CURSOR in PL/SQL
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
eWeek
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Working with REF CURSOR in PL/SQL
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 123
    2007-02-12

    Table of Contents:
  • Working with REF CURSOR in PL/SQL
  • Working with RECORD and REF CURSOR
  • Working with more than one query with the same REF CURSOR
  • Dealing with REF CURSOR in the sub-programs of a PL/SQL block

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    The Web Buyer's Guide is your best source for white papers on a wide range of IT products and services. This Week's Featured White Papers: Guide to Virtual Infrastructure Implementation by VMware

    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.

    More Oracle Articles
    More By Jagadish Chatarji


       · 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
     

       

    ORACLE ARTICLES

    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB
    - Focusing on Templates in Oracle HTML DB




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway