Oracle
  Home arrow Oracle arrow Page 4 - Database Interaction with PL/SQL: Expl...
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

Database Interaction with PL/SQL: Explict Cursors in Depth
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2005-08-02

    Table of Contents:
  • Database Interaction with PL/SQL: Explict Cursors in Depth
  • Working with more than one cursor
  • How the program works
  • Further approaches to the above program
  • Cursors with parameters – a complicated example
  • Can we use JOINS in cursors?

  • 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

    Database Interaction with PL/SQL: Explict Cursors in Depth - Further approaches to the above program


    (Page 4 of 6 )

    The above program can be solved using just the cursor FOR loop as follows (which is best shown with simplicity):

    declare

      cursor c_emp is

            select ename,deptno from emp;

      cursor c_dept is

            select deptno,dname from dept;

    begin

      for r_dept in c_dept

      loop

            dbms_output.put_line(r_dept.dname);

            dbms_output.put_line('-----------------');

            for r_emp in c_emp

            loop

                  if r_emp.deptno = r_dept.deptno then

                        dbms_output.put_line(r_emp.ename);

                  end if;

            end loop;

      end loop;

    end;

    I hope will agree with me that the above approach is very simple when compared with the program demonstrated in the first section. 

    Cursors with parameters – a simple example

    In all of the above programs, the ‘if’ statement looks bit awkward. If no employee exists in a particular department, still the inner loop iterates through all employees, which is totally unnecessary. That means I need to retrieve only the employees I want (instead of all employees). This is where the concept of “cursors with parameters” comes in.  First of we shall see a very simple example of using “cursors with parameters”:

    declare

      cursor c_emp(v_deptno   emp.deptno%type) is

            select ename from emp where deptno=v_deptno;

      v_ename     emp.ename%type;

    begin

      open c_emp(10);

      loop

            fetch c_emp into v_ename;

            exit when c_emp%notfound;

            dbms_output.put_line(v_ename);

      end loop;

      close c_emp;

    end;

    Within the above program, the cursor ‘c_emp’ is declared with a parameter ‘v_deptno’. That means we need to provide a value to that variable (v_deptno) at the time of opening the cursor, which gets directly substituted within the WHERE condition of the SELECT statement of cursor. The cursor FOR loop version of the same program as above will be as follows:

    declare

      cursor c_emp(v_deptno   emp.deptno%type) is

            select ename from emp where deptno=v_deptno;

    begin

      for v_emp in c_emp(10)

      loop

            dbms_output.put_line(v_emp.ename);

      end loop;

    end;

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my in-depth article on working with explicit cursors in PL/SQL....
       · Thank you very much for your in depth look at cursors. It helps me see what else can...
     

       

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