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 
Moblin 
JMSL Numerical Library 
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 / 36
    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


    Database Interaction with PL/SQL, Explicit Cursors - Retrieving More Than One Value With FETCH


    (Page 4 of 4 )

    In all of the above examples, we are trying to fetch only two columns of values ('empno' and 'ename'). We can retrieve more than one value (or entire row) using FETCH statement. Let us consider the following example to fetch an entire row from FETCH into a single variable.

    Declare
      CURSOR c_emp IS
        select * from emp;
      r_emp emp%rowtype;
    Begin
      OPEN c_emp;
      Loop
        FETCH c_emp into r_emp;
        Exit when c_emp%NOTFOUND;
        Dbms_output.put_line(r_emp.empno || ', ' || r_emp.ename);
      End loop;
      CLOSE c_emp;
    End;

    In the above example we are using 'SELECT *' statement to retrieve all columns of information. 'r_emp' is declared as of type 'emp%rowtype'. That means it can store an entire row with the structure available in the table 'emp'. We directly used only a single variable 'r_emp' with FETCH statement (as it can hold an entire row). We displayed the necessary values using dot notation (as demonstrated in the DBMS_OUTPUT statement).

    We can also retrieve only specified columns (without declaring too many variables) using TYPE and RECORD declarations as explained in part-2 of the series. Let us examine that with a simple example:

    Declare
      TYPE t_emprec IS RECORD
      (
        ename emp.ename%type,
        sal   emp.sal%type,
        job   emp.job%type
      );
      r_emp   t_emprec;
      CURSOR c_emp IS
        select ename,sal,job from emp;
    Begin
      OPEN c_emp;
      Loop
        FETCH c_emp into r_emp;
        Exit when c_emp%NOTFOUND;
        Dbms_output.put_line(r_emp.ename || ', ' || r_emp.sal || ', ' || r_emp.job);
      End loop;
      CLOSE c_emp;
    End;

    Another wonder is that we can also declare a variable which is directly based on the structure of an existing cursor as shown in the following example:

    Declare
      CURSOR c_emp IS
        select ename,sal,job from emp;
      r_emp c_emp%rowtype;
    Begin
      OPEN c_emp;
      Loop
        FETCH c_emp into r_emp;
        Exit when c_emp%NOTFOUND;
        Dbms_output.put_line(r_emp.ename || ', ' || r_emp.sal || ', ' || r_emp.job);
      End loop;
      CLOSE c_emp;
    End;

    In the above program the most important declaration is as follows:

    r_emp c_emp%rowtype;

    The above declaration says that a variable 'r_emp' should have the same structure as of cursor 'c_emp' to hold the values. It can hold an entire row from 'c_emp'. This type of syntax is quite widely used by PL/SQL developers. As there exists several approaches of using explicit cursor, I leave it to the readers to choose the best approach which suits them.


    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.

       · 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...
       · thanks a lot for providing the concept of explicit cursors and defntly readers would...
     

       

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