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
THENis required. ELSEIFis written without a space.END IFis written with a space.- The
THENorELSEclause can contain multiple statements. You can (but don’t have to) use aBEGIN … ENDblock to enclose them. - If cond =
NULL, it is treated asFALSE.
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 executedsql
CALL mkw_doku.if_proc(2);Procedure 'mkw_doku.if_proc' was successfully executed