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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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, Explicit Cursors
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 35
    2005-07-26

    Table of Contents:
  • Database Interaction with PL/SQL, Explicit Cursors
  • Working With Explicit Cursor
  • Other Approaches of Using Explicit Cursor
  • Retrieving More Than One Value With FETCH

  • 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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Database Interaction with PL/SQL, Explicit Cursors - Other Approaches of Using Explicit Cursor


    (Page 3 of 4 )

    There are several approaches to work with explicit cursors. The approach given above is a very traditional approach being followed from very early versions of Oracle. Now, let us try another approach.

    Declare
      CURSOR c_emp IS
        select empno, ename from emp;
      v_empno emp.empno%type;
      v_ename emp.ename%type;
    Begin
      OPEN c_emp;
      FETCH c_emp into v_empno,v_ename;
      WHILE c_emp%FOUND
      Loop
        Dbms_output.put_line(v_empno || ', ' || v_ename);
        FETCH c_emp into v_empno,v_ename;
      End loop;
      CLOSE c_emp;
    End;

    The above program looks almost similar to the previous section, except that I am using a WHILE loop with '%FOUND' cursor attribute. Let us consider another approach using FOR loop:

    Declare
      CURSOR c_emp IS
        select empno, ename from emp;
    Begin
      FOR r_emp in c_emp
      Loop
        Dbms_output.put_line(r_emp.empno || ', ' || r_emp.ename);
      End loop;
    End;

    In the case of the above program, the FOR statement itself opens the cursor 'c_emp' and closes after completion. We need not specifically open the cursor in the case of FOR statement. But another issue is that I am using 'r_emp' with FOR loop, which is never declared. PL/SQL allows it! The variables used with the FOR statement (in the form of IMPLICIT cursor) need not be declared. All the values of the row get stored within 'r_emp'. We extract each of those values using dot notation (as demonstrated within the DBMS_OUTPUT statement above).

    We can also restrict number of rows being displayed as demonstrated in the following example:

    Declare
      CURSOR c_emp IS
        select empno, ename from emp;
      v_empno emp.empno%type;
      v_ename emp.ename%type;
    Begin
      OPEN c_emp;
      FETCH c_emp into v_empno,v_ename;
      WHILE c_emp%FOUND and c_emp%rowcount <=5
      Loop
        Dbms_output.put_line(v_empno || ', ' || v_ename);
        FETCH c_emp into v_empno,v_ename;
      End loop;
      CLOSE c_emp;
    End;

    The %ROWCOUNT cursor attribute is explained in my previous article (part-9). It just counts the number of rows fetched. I hope the rest is the same. If you are not using WHILE loop or FOR loop (like in the first example), we can replace that EXIT WHEN statement using the following statement:

    Exit when c_emp%NOTFOUND or c_emp%rowcount > 5;

    If you carefully observe, the WHILE loop condition is quite opposite to that of EXIT WHEN statement.

    More Oracle Articles
    More By Jagadish Chatarji


       · just easy and explained article about new topic i heard about it very often. thnx to...
       · Apart from bit late in replying, I am very happy with the compliments. thanks.
       · Thanks Jagdish for explaining the concepts in a simple way. I am looking forward for...
     

       

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