if

SQL/PL

If – then – else statement

Syntax

if_stmt:
          IF conditional_expr THEN statement_list
          [ELSEIF conditional_expr THEN statement_list]...
          [ELSE statement_list]
          END  IF

Description

If the IF conditional_expr evaluates to true then the statement_list following its associated THEN is executed. If the IF conditional_expr is not true, then first ELSEIF conditional_expr is evaluated and if it is true then the statement_list following its associated then are executed and so on with each ELSEIF clause. If none of the IF or ELSEIF conditional_expr's evaluate to true then the statement_list following the ELSE is executed. At most only one statement_list is executed.

Note that in SQL comparisons can potentially produce three results: true, false, and indeterminate. Indeterminate is returned when null values are used in comparisons other than "IS [NOT] NULL". This means that a condition_expr is not true when it is either false or indeterminate.

Example

if n < 0 then
    set x = n * pi();
    set i = -1;
elseif n = 0 then
    set x = pi();
    set i = 0;
elseif n = 1 then
    set x = pi() / 4.0;
    set i = 1;
elseif n < 10 then
    set i = 10 * n;
    if n < 3 then
        set x = 3.0 * pi();
    elseif n < 7 then
        set x = 7.0 * pi();
    else
        set x = 10.0 * pi();
    end if;
else
    set i = 1000;
    set x = 0.0;
end if;      

See Also

CASE