Oracle
  Home arrow Oracle arrow Page 4 - Working with REF CURSOR in PL/SQL
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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 / 132
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Working with REF CURSOR in PL/SQL - Dealing with REF CURSOR in the sub-programs of a PL/SQL block


    (Page 4 of 4 )

    Sub-programs can also be called sub-routines.  These are nothing but the divisions of the main program.  These divisions are named and are executed when they are called by name from the main program.  They will not get executed unless they are called. 

    The following is an 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;
      procedure PrintEmployeeDetails is
      begin
        loop
          fetch c_emp into er;
          exit when c_emp%notfound;
          dbms_output.put_line(er.name || ' - ' || er.sal);
        end loop;
      end;
    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('--------------');
        PrintEmployeeDetails;
        close c_emp;  
      end loop;
    end;

    In the above program, the sub-routine is named "PrintEmployeeDetails."  You can observe that I am executing (or calling) the sub-routine from within the loop as follows:

      for i in (select deptno,dname from dept)
      loop
         
    .
         
    .
         
    PrintEmployeeDetails;
          .
          .
      end loop;

    According to the above loop, the sub-routine gets executed for every iteration, which displays the employee information for the respective department.

    Passing REF CURSOR as parameters to sub-programs

    In the previous section, we already started working with sub-programs (or sub-routines).  In this section, I shall extend the same with the concept of "parameters" (or arguments).  Every sub-program (or sub-routine) can accept values passed to it in the form of "parameters" (or arguments).  Every parameter is very similar to a variable, but gets declared as part of a sub-program.

    Let us consider the following program:

    declare
      type r_cursor is REF CURSOR;
      c_emp r_cursor;
      type rec_emp is record
      (
        name  varchar2(20),
        sal   number(6)
      );
      procedure PrintEmployeeDetails(p_emp r_cursor) is
        er rec_emp;
      begin
        loop
          fetch p_emp into er;
          exit when p_emp%notfound;
          dbms_output.put_line(er.name || ' - ' || er.sal);
        end loop;
      end;
    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('--------------');
        PrintEmployeeDetails(c_emp);
        close c_emp;  
      end loop;
    end;

    From the above program, you can observe the following declaration:

      procedure PrintEmployeeDetails(p_emp r_cursor) is

    In the above declaration, "PrintEmployeeDetails" is the name of the sub-routine which accepts "p_emp" as a parameter (of type "r_cursor") and we can use that parameter throughout that sub-routine.

    I hope you enjoyed the article and any comments, suggestions, feedback, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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..ThanksJeg
     

       

    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 2 hosted by Hostway