Oracle
  Home arrow Oracle arrow Database Interaction with PL/SQL, part 3
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? 
ORACLE

Database Interaction with PL/SQL, part 3
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2005-06-07


    Table of Contents:
  • Database Interaction with PL/SQL, part 3
  • Using TABLE Without Interacting With Database
  • Combining TABLE and RECORD
  • NESTED TABLES and PL/SQL
  • Using DML Commands on NESTED TABLES Within PL/SQL

  • 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 3
    ( Page 1 of 5 )

    Jagadish Chatarji has been writing about database interactions with Oracle PL/SQL. The last part started on TYPE, RECORD, and TABLE declarations of PL/SQL. This one now goes further into TABLE, RECORD, and using them together. It will also introduce NESTED TABLES.

    Please note that all the examples in this series have been tested only with Oracle 10g, not with all the previous versions of Oracle.  I suggest you to refer the documentation of respective version you are using if any of these programs fails to execute.

    Accessing More Than One Row in PL/SQL (Using WHILE)

    In our previous article, we have seen how to display more than one row using a FOR loop. Now, we will see the same program being used with WHILE loop. Let us go through the following example first.

    declare
        type t_emptbl is table of emp%rowtype;
        v_emptbl    t_emptbl;
        i           integer;
    begin
       
    select * bulk collect into v_emptbl from emp;
       
    i := v_emptbl.first;
        while i is not null
       
    loop
           
    dbms_output.put_line(v_emptbl(i).ename || ' earns '
              || v_emptbl(i).sal);
           
    i := v_emptbl.next(i);
        end loop;
    end;

    In the above program, all the declarations and other parts are identical to that of the previous article, except that we used WHILE loop here. You may be wondering if the FOR loop is simpler than the WHILE loop. And of course, I admit it is. But by using this example, I wanted to introduce the collection method NEXT which can be used with a TABLE typed variable (whereas it may not be necessary when using a FOR loop).

    In the above program, we use a variable 'i' as a counter. We initialize it using 'v_emptbl.first' and we increment it using 'v_emptbl.next(i)'. If no more entries are available (beyond last index), it returns NULL into the variable 'i', which is being checked as a condition of the WHILE loop.

    We can also display the result in reverse using collection method PRIOR (in combination with collection method LAST as initial value of index) as shown below.

    declare
        type t_emptbl is table of emp%rowtype;
       
    v_emptbl    t_emptbl;
       
    i           integer;
    begin
       
    select * bulk collect into v_emptbl from emp;
       
    i := v_emptbl.last;
       
    while i is not null
       
    loop
           
    dbms_output.put_line(v_emptbl(i).ename || ' earns '
              || v_emptbl(i).sal);
           
    i := v_emptbl.prior(i);
       
    end loop;
    end;



     
     
    >>> 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 3 Hosted by Hostway
    Stay green...Green IT