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

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:

     TYPE tipe_arr_nilai IS TABLE OF NUMBER 
arr_nilai tipe_arr_nilai;
total NUMBER;
     arr_nilai(1) := 1000;
     arr_nilai(2) := 2000;
arr_nilai(3) := 3000;
total := arr_nilai(1) + arr_nilai(2) + arr_nilai(3);

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.

No comments

Powered by Blogger.