Oracle
  Home arrow Oracle arrow Page 3 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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


    Database Interaction with PL/SQL: Explict Cursors in Depth - How the program works


    (Page 3 of 6 )

    Let me explain each of the statements individually.

      cursor c_emp is

            select ename,deptno from emp;

    The above statement declares a cursor ‘c_emp’ which can hold all employee names and department numbers from the table ‘emp’.

    cursor c_dept is

            select deptno,dname from dept;

    The above statement declares a cursor ‘c_dept’ which can hold all department numbers and department names from the table ‘dept’.

    r_emp   c_emp%rowtype;

      r_dept      c_dept%rowtype;

    The above two statements declare two variables, each based on its own cursor type. That means ‘r_emp’ can hold both the employee name and the department number at one time; similarly, ‘r_dept’ can hold both department number and department name.  Each of those variables can hold only one row (one set of values) from their respective cursors at a time.

    open c_dept;

      loop

            fetch c_dept into r_dept;

            exit when c_dept%notfound;

    The first statement opens the cursor ‘c_dept’. We start a loop to fetch each and every row from ‘c_dept’. We fetch each and every successive row from ‘c_dept’ into ‘r_dept’ (for every iteration). And finally we exit the loop, if the fetch could not find any row to fetch.

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

            dbms_output.put_line(r_dept.dname);

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

    The above statements display the current department name fetched in the form of a heading.

    open c_emp;

            loop

                  fetch c_emp into r_emp;

                  exit when c_emp%notfound;

    Now, we fetch each and every row from cursor ‘c_emp’ into ‘r_emp’ using a loop.

    if r_emp.deptno = r_dept.deptno then

            dbms_output.put_line(r_emp.ename);

      end if;

    I am checking whether the ‘deptno’ of ‘r_dept’ (currently related to the outer loop) is matching with the ‘deptno’ of ‘r_emp’ or not. If it matches, the employee belongs to that department. So we immediately display the employee name.

    end loop;

            close c_emp;

      end loop;

      close c_dept;

    end;

    Finally, we close each of the cursors, based on the termination of the respective loops. 

    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

    - Implementing and Using Oracle`s Restore Poin...
    - 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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT