Assigning values to arrays using subindices and literal values

Once an associative array variable has been created or declared, values can be assigned to it. One way of assigning values to associative arrays is by direct assignment.

Before you begin

  • Read: Associative array data types
  • Read: Restrictions on associative array data types
  • Ensure that an associative array variable is in the current scope of use.

About this task

Assigning values to associative array variable elements can be done by using the assignment statement in which the array is named, the index value is specified and the corresponding element value is assigned.

Procedure

  1. Define the assignment statement for an associative array variable.
    • Specify the variable name, the index value, and the element value.
    • Specify another associative array variable.
  2. Execute the assignment statement from a supported interface.

Example

Example 1:
The following is an example of a variable declaration and a series of assignment statements that define values in the array:
DECLARE capitals capitalsArray;

SET capitals['British Columbia'] = 'Victoria';
SET capitals['Alberta'] = 'Edmonton';
SET capitals['Manitoba'] = 'Winnipeg';
SET capitals['Ontario'] = 'Toronto';
SET capitals['Nova Scotia'] = 'Halifax';

In the capitals array, the array index values are province names and the associated array element values are the names of the corresponding capital cities. Associative arrays are sorted in ascending order of index value. The order in which values are assigned to associative array elements does not matter.

Example 2:
An associative array variable can also be assigned an associative array variable value of the same associative array data type. This can be done using the assignment statement. For example, consider two associative array variables, capitalsA and capitalsB defined as:
DECLARE capitalsA capitalsArray;
DECLARE capitalsB capitalsArray;

SET capitalsA['British Columbia'] = 'Victoria';
SET capitalsA['Alberta'] = 'Edmonton';
SET capitalsA['Manitoba'] = 'Winnipeg';
SET capitalsA['Ontario'] = 'Toronto';
SET capitalsA['Nova Scotia'] = 'Halifax';
The variable capitalsB can be assigned the value of the variable capitalsA by executing the following assignment statement:
SET capitalsB = capitalsA;
Once executed, capitalsB will have the same value as capitalsA.

What to do next

If the assignment statement executes successfully, the value has been successfully assigned and the new variable value can be referenced.

If the statement failed to execute successfully, verify and correct the SQL statement syntax and verify that the variables named are defined before executing the statement again.