Oracle
  Home arrow Oracle arrow Page 2 - Associative Arrays in Oracle PL/SQL: The Professional Approach
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? 
Google.com  
ORACLE

Associative Arrays in Oracle PL/SQL: The Professional Approach
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 18
    2006-03-20


    Table of Contents:
  • Associative Arrays in Oracle PL/SQL: The Professional Approach
  • A professional approach to traversing an associative array using Oracle PL/SQL
  • Bottom to top traverse of an associative array using Oracle PL/SQL
  • Deleting individual elements in an associative array using Oracle PL/SQL
  • Deleting a set of elements in an associative array using Oracle 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


    Associative Arrays in Oracle PL/SQL: The Professional Approach - A professional approach to traversing an associative array using Oracle PL/SQL
    ( Page 2 of 5 )

    Let us concentrate on the problems with my previous approaches.  There exist mainly two issues to deal with. 

    The first is that in all of the examples in my previous article, we should definitely know (or expect) the “least possible key” and the “highest possible key,” which is quite ridiculous.  

    The second is that if I have huge gaps in between “keys,” the loop gets iterated unnecessarily (degrading the performance).  This is another major disadvantage of using my previous methods.

    The following example should definitely solve all those issues.  Let us first go through the code:

    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;

          i := year_sales.first;
          while i <= year_sales.last
          loop
                tot_sales := tot_sales + year_sales(i);
                dbms_output.put_line('Sales of ' || i || ': ' ||
    year_sales(i));
                i := year_sales.next(i);
          end loop;
          dbms_output.put_line('Total sales: ' || tot_sales);
          dbms_output.put_line('Avg sales: ' ||
    (tot_sales/year_sales.count) );

    end;

    I shall explain the above code in the next section.

    A professional approach to traversing an associative array using Oracle PL/SQL: discussion

    The explanation in this section is entirely based on the code listed in the previous section.  Let me explain the same part by part.

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

    The “year_type” is a user-defined data type which can hold a set (or table) of values (typically of type “number”). It is 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.”   

    Further proceeding we have the statements which add some data to the associative array.  Then we have the following:

          i := year_sales.first;

    The above statement gives the first “key” available within the associative array.  The next statement is as follows:

          while i <= year_sales.last

    The above is simply a loop which always checks and maintains the current “key” so that it would never cross the last “key.”  Further proceeding we have the following:

                tot_sales := tot_sales + year_sales(i);
                dbms_output.put_line('Sales of ' || i || ': ' ||
    year_sales(i));

    The first statement simply gets the “value” of the pair based on the “key” present in the variable “i” and finally adds up the value with “tot_sales” (which was initialized to zero earlier).  The second statement would simply display the value of “key” and “value” together in a separate line.

                i := year_sales.next(i);

    The above statement searches for the next “key” from the current “key” available in variable “i.”  The new “key” would be assigned to the same variable and the loop gets iterated accordingly.

    Even though the concept is totally rewritten, one should agree now that all the requirements are met professionally!



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek