Oracle
  Home arrow Oracle arrow Page 4 - Database Interaction with PL/SQL, part 2
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
ORACLE

Database Interaction with PL/SQL, part 2
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 18
    2005-05-31


    Table of Contents:
  • Database Interaction with PL/SQL, part 2
  • UPDATE with RETURNING clause
  • TYPE with RECORD declaration
  • Accessing more than one row in PL/SQL (TYPE with TABLE)

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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, part 2 - Accessing more than one row in PL/SQL (TYPE with TABLE)
    ( Page 4 of 4 )

    In all of the previous sample programs, we discussed retrieving and manipulating only a single row. How about retrieving more than one (or a set of rows based on a SELECT statement)? Before going into too many explanations, let us go through the following beautiful example:

    declare

    type t_emptbl is table of emp%rowtype;

    v_emptbl t_emptbl;

    begin

    select * bulk collect into v_emptbl from emp;

    for i in v_emptbl.first .. v_emptbl.last

    loop

    dbms_output.put_line(v_emptbl(i).ename || ' earns ' || v_emptbl(i).sal);

    end loop;

    end;

    The above program displays all of the employee names with their respective salaries. Here I introduced a new declaration, TYPE..IS TABLE. Let me explain the above program part by part.

    type t_emptbl is table of emp%rowtype;

    The above statement defines a new data type, "t_emptbl" as an in-memory table representation. This means that it can store any number of rows (just like a table) from the table "emp" (as we included "emp%rowtype") in memory (without having any relation to the physical database). Make sure that it is only a data type. If you know the C language, you can think of it as a structured array.

    v_emptbl t_emptbl;

    The above statement declares a variable "v_emptbl" which is authorized to store information based on the table type defined by "t_emptbl."

    select * bulk collect into v_emptbl from emp;

    This is the most important statement, which does all of the operations. If you remove BULK COLLECT, it would raise an error (because you cannot work with more than one row at a time). The BULK COLLECT lets you fetch any number of rows based on the SELECT statement issued and store all of them in a TABLE typed variable. From the above statement, it is quite clear that we are retrieving all of the rows from the table "emp" and collecting them into the TABLE typed variable "v_emptbl."

    Now the next issue is to fetch all those rows of values and display them back on the screen. In general, each and every row of the TABLE typed variable is indexed, starting from one (just like an array). We retrieve each and every row with that index, using a FOR loop.

    for i in v_emptbl.first .. v_emptbl.last

    loop

    dbms_output.put_line(v_emptbl(i).ename || ' earns ' || v_emptbl(i).sal);

    end loop;

    The above part is a FOR loop that starts from the first index of the TABLE typed variable "v_emptbl" and ends at the last index of the same. Here "first" and "last" are keywords which can be used with any TABLE typed variables. We retrieve each and every field of information by using the notation TABLE(INDEX).FIELDNAME (which is done in the DBMS_OUTPUT statement).



     
     
    >>> More Oracle Articles          >>> More By Jagadish Chatarji
     

       

    ORACLE ARTICLES

    - Oracle's Turn to Play in the Sun
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek