Associative Arrays v11
An associative array is a type of collection that associates a unique key with a value. The key does not have to be numeric, but can be character data as well.
An associative array has the following characteristics:
- An associative array type must be defined after which array variables can be declared of that array type. Data manipulation occurs using the array variable.
- When an array variable is declared, the associative array is created, but it is empty - just start assigning values to key values.
- The key can be any negative integer, positive integer, or zero if
INDEX BY BINARY_INTEGER
orPLS_INTEGER
is specified. - The key can be character data if
INDEX BY VARCHAR2
is specified. - There is no pre-defined limit on the number of elements in the array - it grows dynamically as elements are added.
- The array can be sparse - there may be gaps in the assignment of values to keys.
- An attempt to reference an array element that has not been assigned a value will result in an exception.
The TYPE IS TABLE OF ... INDEX BY
statement is used to define an associative array type.
assoctype
is an identifier assigned to the array type. datatype
is a scalar data type such as VARCHAR2
or NUMBER
. rectype
is a previously defined record type. objtype
is a previously defined object type. n
is the maximum length of a character key.
In order to make use of the array, a variable must be declared with that array type. The following is the syntax for declaring an array variable.
array
is an identifier assigned to the associative array. assoctype
is the identifier of a previously defined array type.
An element of the array is referenced using the following syntax.
array
is the identifier of a previously declared array. n
is the key value, type-compatible with the data type given in the INDEX BY
clause. If the array type of array
is defined from a record type or object type, then [.field ]
must reference an individual field within the record type or attribute within the object type from which the array type is defined. Alternatively, the entire record can be referenced by omitting [.field ]
.
The following example reads the first ten employee names from the emp
table, stores them in an array, then displays the results from the array.
The above example produces the following output:
The previous example is now modified to use a record type in the array definition.
The following is the output from this anonymous block.
The emp%ROWTYPE
attribute could be used to define emp_arr_typ
instead of using the emp_rec_typ
record type as shown in the following.
The results are the same as in the prior example.
Instead of assigning each field of the record individually, a record level assignment can be made from r_emp
to emp_arr
.
The key of an associative array can be character data as shown in the following example.