Creating an associative array data type is a task that
you would perform as a prerequisite to creating a variable of the
associative array data type. Associative array data types are created
by executing the CREATE TYPE (array) statement.
Before you begin
Ensure you have the privileges required to execute the CREATE
TYPE statement.
About this task
Associative array data types can only be used in certain
contexts.
Procedure
- Define the CREATE TYPE statement:
- Specify a name for the associative array data type.
A good name is one that clearly specifies the type of data stored
in the array. For example: Products might be a good name for an
array that contains information about products where the array index
is the product identifier. As another example, the name y_coordinate
might be a good name for an array where the array index is the x coordinate
value in a graph function.
- Specify the AS keyword followed by the keyword name
for the data type of the array elements (e.g. INTEGER).
- Specify the ARRAY keyword.
Within the square
brackets of the ARRAY clause, specify the data type of the array index.
Note: With associative arrays, there is no explicit limit on the
number of. elements or on the domain of the array index values.
- Execute the CREATE TYPE statement from a supported interface.
Example
Example 1:
The following is an example
of a CREATE TYPE statement that creates an array named assocArray with
20 elements and a array index of type VARCHAR.CREATE TYPE assocArray AS INTEGER ARRAY[VARCHAR(20)];
Example
2:
The following is an example of a basic associative
array definition that uses the names of provinces for indices and
where the elements are capital cities: CREATE TYPE capitalsArray AS VARCHAR(12) ARRAY[VARCHAR(16)];
What to do next
If the statement executes successfully the array data type
is created in the database and the array data type can be referenced..After
creating the array data type you might want to create an associative
array variable.