Oracle
  Home arrow Oracle arrow Page 2 - 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Associative Arrays in Oracle PL/SQL: Introduction - Associative Arrays in Oracle PL/SQL: a simple example
    (Page 2 of 4 )

    I already explained Associative Arrays in the previous section.  Now, we shall work more practically.  Let us consider the following example:

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

          tot_sales := year_sales(1990) + year_sales(1991) +
    year_sales(1992);
          dbms_output.put_line('Total sales: ' || tot_sales);
    end;

    I shall explain the above example part by part.  Let us start with the following statements first:

          type year_type is table of number index by binary_integer;
          year_sales year_type;

    Repeating from the previous section, the “year_type” is a user-defined data type which can hold a set (or table) of values (typically of type “number”), organized with a BINARY_INTEGER index. 

    The “year_sales” is a variable based on the data type “year_type.”  Now you can store a huge amount of data (typically in the form of pairs) within the single variable “year_type.”  The pair of data should now contain a “key” (of type BINARY_INTEGER) and a “value” (of type NUMBER). Further proceeding we have the following statements:

          year_sales(1990) := 34000;
          year_sales(1991) := 45000;
          year_sales(1992) := 43000;

    From the above three statements, you can simply consider that 1990, 1991 and 1992 are “keys” (of type BINARY_INTEGER).  Similarly, we have 34000, 45000 and 43000 which are simply “values” (of type NUMBER). So, we added three pairs of data to a single variable, “year_sales.”  That’s the trick.  Further proceeding we have the following:

          tot_sales := year_sales(1990) + year_sales(1991) +
    year_sales(1992);
          dbms_output.put_line('Total sales: ' || tot_sales);

    The first statement simply retrieves the “values” available at “keys” 1990, 1991 and 1992 (which are nothing but 34000, 45000 and 43000) and finally adds them into a new simple variable, “tot_sales” (of type NUMBER).  The second statement simply displays the result. 

    The above example is the simplest on earth.  Let’s try something a bit harder.

    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