Skip to content

IF

The IF statement allows executing one or more statements when the condition evaluates to TRUE.

IF is only valid within a scripting block.

See also "IF construct" in "Conditional logics".

Syntax

sql
IF (cond) THEN
    statement;
    [statement; ... ]
[
ELSEIF (cond) THEN
    statement;
    [statement; ... ]
]
[
ELSE
    statement;
    [statement; ... ]
]
END IF;

Arguments

cond: An expression that returns a BOOLEAN value.


statement: One (or more) statements of the following types:

  • A single SQL statement (including CALL).
  • A control flow statement (e.g. loop or conditional).
  • A nested block.

Usage Notes

  • The keyword THEN is required.
  • ELSEIF is written without a space.
  • END IF is written with a space.
  • The THEN or ELSE clause can contain multiple statements. You can (but don’t have to) use a BEGIN … END block to enclose them.
  • If cond = NULL, it is treated as FALSE.

Example

sql
CREATE OR REPLACE PROCEDURE mkw_doku.if_proc(condition_value INTEGER)
RETURNS STRING
AS
BEGIN
    IF (condition_value = 1) THEN
        RETURN 'first';
    ELSEIF (condition_value = 2) THEN
        RETURN 'second';
    ELSE
        RETURN 'Not defined.';
    END IF;
END;
;

CALL mkw_doku.if_proc(3);
Procedure 'mkw_doku.if_proc' was successfully executed
sql
CALL mkw_doku.if_proc(2);
Procedure 'mkw_doku.if_proc' was successfully executed