Oracle
  Home arrow Oracle arrow Database Interaction with PL/SQL, part...
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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


       · Hello guys, this is my article on working with database information (several other...
       · Author mentions that subscript starts from 0. trying to access v_numtbl(0) results...
       · hi,I've gone through some of your articles.They are really an excellent 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 5 hosted by Hostway