DB2 Version 9.7 for Linux, UNIX, and Windows

Determining if an array element exists

Determining if an array element exists and has a value is a task that can be done using the ARRAY_EXISTS function.

About this task

You would perform this task within SQL PL code in order to determine if an array element exists within an array.

Before you begin

Procedure

  1. Define an IF statement:
    1. Define a condition that includes the ARRAY_EXISTS function.
    2. Specify the THEN clause and include any logic that you want to have performed if the condition is true and add any ELSE caluse values you want.
    3. Close the IF statement with the END IF clause.
  2. Execute the IF statement.

Results

Example

For an array of phone numbers defined as:
phones		index		0		           1		           2		           3 				
         phone		'416-223-2233'	'416-933-9333'	'416-887-8887'	'416-722-7227'
After executing the following, the variable x will be set to 1.
IF (ARRAY_EXISTS(phones, 2)) THEN
  SET x = 1;
END IF;  

What to do next

If the SET statement failed to execute successfully: