Oracle
  Home arrow Oracle arrow Page 3 - Associative Arrays in Oracle PL/SQL: I...
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

Associative Arrays in Oracle PL/SQL: Introduction
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 62
    2006-03-13

    Table of Contents:
  • Associative Arrays in Oracle PL/SQL: Introduction
  • Associative Arrays in Oracle PL/SQL: a simple example
  • Associative Arrays in Oracle PL/SQL: traversing consecutively using a FOR loop
  • Associative Arrays in Oracle PL/SQL: counting number of elements using FOR loop

  • 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

    Associative Arrays in Oracle PL/SQL: Introduction - Associative Arrays in Oracle PL/SQL: traversing consecutively using a FOR loop
    (Page 3 of 4 )

    The example in the previous section simply contains three pairs of values.  If I have tons of pairs, how would I retrieve the values?  This section addresses that issue.

    Let us consider the following example first:

    declare
          type year_type is table of number index by binary_integer;
          year_sales year_type;
          tot_sales   number := 0;
          i           number;
    begin
          year_sales(1990) := 34000;
          year_sales(1991) := 45000;
          year_sales(1992) := 43000;
          year_sales(1993) := 13000;
          year_sales(1994) := 53000;

          for i in 1990..1994
          loop
                tot_sales := tot_sales + year_sales(i);
                dbms_output.put_line('Sales of ' || i || ': ' ||
    year_sales(i));
          end loop;
          dbms_output.put_line('Total sales: ' || tot_sales);

    end;

    Within the above example, I added a few more pairs of data and used a FOR loop to traverse through every pair within the “Associative Array.”  According to the above example, we should know the “least key” and “highest key.” 

    Here, the loop variable acts as the main “key” holder and repeats through all the “keys” with a default increment of 1. The “keys” must be consecutive to work with the above example.

    Associative Arrays in Oracle PL/SQL: traversing and jumping within FOR loop

    No programmer would ever be satisfied with the FOR loop I demonstrated within the previous section.  The example in the previous section works, but only if you work with “consecutive numbering” or “consecutive keys,” which is not the situation every time in databases!

    Sometimes the “keys” may not be in any order.  And sometimes, a few “keys” may be missing!  How do we handle such situations?  I address these issues in this section.  Let us modify the previous example, so that it reaches our expectations.

    declare
          type year_type is table of number index by binary_integer;
          year_sales year_type;
          tot_sales   number := 0;
          i           number;
    begin
          year_sales(1990) := 34000;
          year_sales(1991) := 45000;
          year_sales(1992) := 43000;
          year_sales(1996) := 13000;
          year_sales(1998) := 53000;

          for i in 1990..2000
          loop
                if year_sales.exists(i) then
                      tot_sales := tot_sales + year_sales(i);
                      dbms_output.put_line('Sales of ' || i || ': '
    || year_sales(i));
                end if;
          end loop;
          dbms_output.put_line('Total sales: ' || tot_sales);

    end;

    This time I worked more intelligently.  I started from the “least key” and went through the “highest key.”  I am testing whether the “key” really exists in my “Associative Array” or not, using an IF condition as follows:

                if year_sales.exists(i) then

    The keyword “exists” is a pre-defined member to work with any “Associative Array.”  It simply tests whether the “key” exists or not and sends out a “Boolean” value.  Now, our “keys” could be in any order and we can even miss several “keys” in between.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hai everybody. This is going to be another series on working with "Associative...
       · On the positive side, the article is pretty clear on using sparse arrays in PL/SQL....
       · Hello, I may be wrong when I said "I am confident that it works in 7.3 as well". ...
     

       

    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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway