Indicator variables in applications that use SQL

An indicator variable is a halfword integer variable used to indicate whether its associated host variable has been assigned a null value.

You can also use an indicator variable to verify that a retrieved string value has not been truncated. If truncation occurs, the indicator variable contains a positive integer that specifies the original length of the string. If the string represents a large object (LOB), and the original length of the string is greater than 32767, the value that is stored in the indicator variable is 32767, since no larger value can be stored in a halfword integer.

When the database manager returns a value from a result column, you can test the indicator variable. If the value of the indicator variable is less than zero, you know the value of the results column is null. When the database manager returns a null value, the host variable will be set to the default value for the result column.

You specify an indicator variable (preceded by a colon) immediately after the host variable or immediately after the keyword INDICATOR. For example:
EXEC SQL
   SELECT COUNT(*), AVG(SALARY)
   INTO :PLICNT, :PLISAL:INDNULL
   FROM CORPDATA.EMPLOYEE
   WHERE EDLEVEL < 18
END-EXEC.

You can then test INDNULL to see if it contains a negative value. If it does, you know SQL returned a null value.

Always test for NULL in a column by using the IS NULL predicate. For example:

WHERE expression IS NULL
 

Do not test for NULL in this way:

MOVE -1 TO HUIND.
EXEC SQL...WHERE column-name = :HUI :HUIND
 

The EQUAL predicate will always be evaluated as false when it compares a null value. The result of this example will select no rows.

The DISTINCT predicate can be used to perform comparisons when null values may exist.

Related information
Predicates