Friday, June 28, 2013

23rd Example - Associative Array (Index-By Table) on PL/SQL

Leave a Comment
In this example we will study the use of arrays in PL/SQL block. Array in PL/SQL block is known as collection types. One of the collection types is an associative array. Here is an example of PL /SQL block that have associative arrays:

DECLARE
     TYPE tipe_arr_nilai IS TABLE OF NUMBER 
       INDEX BY BINARY_INTEGER;
arr_nilai tipe_arr_nilai;
total NUMBER;
BEGIN
     arr_nilai(1) := 1000;
     arr_nilai(2) := 2000;
arr_nilai(3) := 3000;
total := arr_nilai(1) + arr_nilai(2) + arr_nilai(3);
DBMS_OUTPUT.PUT_LINE (total);
END;
/


In the declaration part of PL/SQL block above, found TYPE statement. TYPE is used here to form a collection types which will serve as an associative array. TYPE tipe_arr_nilai is an associative array (can be considered as a table) that have attributes with the data type NUMBER. Arr_nilai variables will have tipe_arr_nilai data type (NUMBER set). Inside the BODY of PL/SQL block found arrays arr_nilai 3 (1), arr_nilai (2) and arr_nilai (3), each of them has a different value. The third value of the array can be summed using the sum operator plus (+). Results from PL/SQL block above is the value of 6000.
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment