Oracle
  Home arrow Oracle arrow Page 2 - 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, 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

    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, Explicit Cursors - Working With Explicit Cursor


    (Page 2 of 4 )

    I spoke too much in the previous section without giving any practical example. So, first of all let us go through an 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;
      Loop
        FETCH c_emp into v_empno,v_ename;
        Exit when c_emp%NOTFOUND;
        Dbms_output.put_line(v_empno || ', ' || v_ename);
      End loop;
      CLOSE c_emp;
    End;

    Let me explain the above example step by step.

    CURSOR c_emp IS select empno, ename from emp;

    The above statement defines a cursor with a name 'c_emp' based on the provided SELECT statement. Make sure it is only a declaration and does not process anything yet.

    OPEN c_emp;

    The above statement makes the SELECT statement available with 'c_emp' to execute. When this statement gets executed, all the rows (with only the columns 'empno' and 'ename') from the table 'emp' gets retrieved and stored in a memory context identified by 'c_emp'.

    FETCH c_emp into v_empno,v_ename;

    The above statement fetches only one consecutive row from the memory. FETCH statement in general, moves only one row forward at a time. That is why we need to use a loop. After fetching the row information, the values get assigned to the variables specified.

    Exit when c_emp%NOTFOUND;

    The loop gets terminated based on the above statement. You can also observe that the cursor attribute '%NOTFOUND' is being used with an explicit cursor. If no rows are fetched through the previous FETCH statement of the same cursor 'c_emp', it returns 'true' and the control skips out of the loop.

    Dbms_output.put_line(v_empno || ', ' || v_ename);

    The above statement displays the current values available in 'v_empno' and 'v_ename'.

    CLOSE c_emp;

    The above statement frees up resources by removing all the allocations of cursor 'c_emp' from the memory. A cursor once closed cannot be used anymore, unless we open it once again. And that terminates the whole story. Is it too complicated? I don't think so, except that we need to remember the syntactical issues.

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