Oracle
  Home arrow Oracle arrow Page 3 - Database Interaction with PL/SQL: Nest...
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: Nested Tables
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 35
    2005-06-21

    Table of Contents:
  • Database Interaction with PL/SQL: Nested Tables
  • Working with DML statements using NESTED TABLE with OBJECT TYPE
  • Working with PL/SQL using NESTED TABLE with OBJECT TYPE
  • A Powerful FOR loop in 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

    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: Nested Tables - Working with PL/SQL using NESTED TABLE with OBJECT TYPE


    (Page 3 of 4 )

    PL/SQL nested tables represent sets of values. You can think of them as one-dimensional arrays with no upper bound. You can model multi-dimensional arrays by creating nested tables whose elements are also nested tables.  Within the database, nested tables are column types that hold sets of values. Oracle stores the rows of a nested table in no particular order. When you retrieve a nested table from the database into a PL/SQL variable, the rows are given consecutive subscripts starting at 1. That gives you array-like access to individual rows.

    The PL/SQL would be almost similar to the example presented in part-3 of my article, but with some simple enhancements.  Consider the following example:

    declare
        v_experiences    t_experience_tbl;
        v_name           varchar2(20) := '&name';
    begin
        select experiences into v_experiences
        from employees where name=v_name;
        dbms_output.put_line('Experience list of ' || v_name);
        dbms_output.put_line('-----------------------');
        for i in v_experiences.first .. v_experiences.last
        loop
            dbms_output.put(v_experiences(i).Position);
            dbms_output.put_line (',' || v_experiences(i).NoOfYears);
         end loop;
    end;

    I don’t think there exists anything new from the above program.  You are just creating a TABLE based variable ‘v_experiences’, storing all the values into that variable using the SELECT statement and displaying them using a FOR loop.  It seems quite comfortable to work with single row of information (as above).  Let us consider another issue.  How to display all employees with all their experiences?  The following example shows the solution:

    declare
        v_experiences    t_experience_tbl;
        TYPE t_name is TABLE OF varchar2(20);
        v_name           t_name;

    begin
        select name bulk collect into v_name from employees;
        for j in v_name.first .. v_name.last
        loop
            select experiences into v_experiences
            from employees where name=v_name(j);
            dbms_output.put_line('-----------------------');
            dbms_output.put_line('Experience list of ' || v_name);
            dbms_output.put_line('-----------------------');
            for i in v_experiences.first .. v_experiences.last
            loop
                dbms_output.put(v_experiences(i).Position);
                dbms_output.put_line (',' || v_experiences(i).NoOfYears);
            end loop;
        end loop;
    end;

    In the above program, I declared a new TABLE type ‘t_name’ and a variable ‘v_name’ based on it to hold all employee names as a bulk.  I populated all the employee names into that variable using the first SELECT statement.  I am using two FOR loops.  One loop for the employee names and the other for experience list of employee retrieved by the outer FOR loop.  It is something like the parent-child display.

    The above program is NOT the only solution.  We can develop in several other ways based on the appropriate situation.

    Can we INSERT, UPDATE or DELETE from NESTED TABLES using PL/SQL?  Why not?  We can do it in several ways.  We can use RECORD types, TABLE types or even individual variables to do all such tasks.   The syntax will be almost similar to the examples given in the previous section (SQL statements).  But to be bit enhanced with PL/SQL based variables in whatever way we like.  I leave it to you to try and investigate the maximum number of ways to achieve them.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my article on working with nested tables in PL/SQL. You can...
       · Please check the code for topic, 'Working with PL/SQL using NESTED TABLE with...
     

       

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