Previous Topic: EXEC ADSNext Topic: ITERATE


IF

The IF statement selects different execution paths depending on the evaluation of one or more truth value expressions, given as SQL search conditions.

Syntax
                                        ┌────────────────────────────┐
►►─── IF ── search-condition ── THEN ── ▼ ─ procedure-statement ─ ; ─┴──────────►

 ►─┬───────────────────────────────────────────────────────────────────────────┬►
   │  ┌──────────────────────────────────────────────────────────────────────┐ │
   │  │                                       ┌────────────────────────────┐ │ │
   └─ ▼ ─ ELSEIF ── search-condition ─ THEN ─ ▼ ─ procedure-statement ─ ; ─┴─┴─┘

 ►─┬───────────────────────────────────────────┬─ END IF ───────────────────────►◄
   │            ┌────────────────────────────┐ │
   └─ ELSE ──── ▼ ─ procedure-statement ─ ; ─┴─┘
Parameters
IF search-condition

Specifies the truth value expression to be evaluated. The outcome of the evaluation determines the execution path.

THEN procedure-statement

Specifies the statements to be executed if the immediately preceding search condition is true.

ELSEIF search-condition

Specifies the truth value expression to be evaluated if the outcomes of all previously evaluated search conditions are false.

ELSE procedure-statement

Specifies the statements to be executed if all search conditions are false.

Usage

If no alternative execution path is given, execution continues with the next statement outside the IF.

Example
set options command delimiter '++';
create procedure USER01.TIF1
  ( TITLE   varchar(10) with default
  , P_LEFT    integer
  , P_RIGHT   real
  , RESULT  varchar(30)
  )
    EXTERNAL NAME TIF1 LANGUAGE SQL
Label_200:
begin not atomic
 /*
 ** Compare an integer value with a real value
 */
  if (P_LEFT > P_RIGHT)
    then set RESULT = 'p_left > p_right';
  elseif (P_LEFT = P_RIGHT)
    then set RESULT = 'p_left = p_right';
  elseif  (P_LEFT < P_RIGHT)
    then set RESULT = 'p_left < p_right';
  else   set RESULT = 'p_left and/or p_right NULL !';
  end if;
end
++
commit++
call user01.TIF1('Test IF >', 4, 2)++
*+
*+ TITLE            P_LEFT         P_RIGHT  RESULT
*+ -----            ------         -------  ------
*+ Test IF >             4   2.0000000E+00  P_LEFT >  P_RIGHT
call user01.TIF1('Test IF <', 4, 9)++
*+
*+ TITLE            P_LEFT         P_RIGHT  RESULT
*+ -----            ------         -------  ------
*+ Test IF <             4   9.0000000E+00  P_LEFT <  P_RIGHT

call user01.TIF1('Test IF =', 2, 2)++
*+
*+ TITLE            P_LEFT         P_RIGHT  RESULT
*+ -----            ------         -------  ------
*+ Test IF =             2   2.0000000E+00  P_LEFT = P_RIGHT

call user01.TIF1('Test IF ', 4)++
*+
*+ TITLE            P_LEFT         P_RIGHT  RESULT
*+ -----            ------         -------  ------
*+ Test IF               4          <null>  P_LEFT AND/OR P_RIGHT
NULL !
set options command delimiter default++