Executes one or more SQL statements conditionally. It then continues processing by executing any SQL statement that immediately follows the block.
1 – Environment
You can use the IF control statement in a compound statement: o In interactive SQL o Embedded in host language programs to be precompiled o As part of a procedure in an SQL module o In dynamic SQL as a statement to be dynamically executed
2 – Format
if-statement = -----> IF predicate THEN --+-> compound-use-statement -+--------+ +------------ <-------------+ | +---------------------------------------------------------------+ ++-+--------------------------------------------------------+-+-+ | +-> ELSEIF predicate THEN -+-> compound-use-statement -+-+ | | | +------------ <-------------+ | | +---------------------------- <------------------------------+ | +---------------------------------------------------------------+ +--+----------------------------------------+--> END IF ---------> +-> ELSE --+-> compound-use-statement -+-+ +------------ <-------------+
3 – Arguments
3.1 – compound-use-statement
See the Compound_Statement HELP topic for a description of the SQL statements that are valid in a compound statement.
3.2 – END_IF
Marks the end of an IF statement. Every IF statement must end with the END IF clause.
3.3 – ELSE compound use statement
Executes one or more SQL statements associated with the ELSE clause but only when the value of the IF and ELSEIF predicates evaluate to FALSE or UNKNOWN.
3.4 – ELSEIF_THEN
If the ELSEIF predicate evaluates to TRUE, SQL executes the SQL statements in the THEN clause. If the ELSEIF predicate does not evaluate to TRUE, SQL evaluates the predicates in any subsequent ELSEIF or ELSE clauses.
3.5 – IF_THEN
Executes one or more SQL statements in an IF . . . END IF block only when the value of an IF predicate evaluates to TRUE. A predicate, also called a conditional expression, specifies a condition that SQL evaluates to TRUE, FALSE, or UNKNOWN. If the predicate evaluates to TRUE, SQL executes the statement in the THEN clause. If the predicate does not evaluate to TRUE, SQL evaluates the predicate in any ELSEIF clauses. If the IF statement contains no ELSEIF clauses, SQL executes any statements in the ELSE clause.
3.6 – predicate
See the Predicates HELP topic for more information on predicates.
4 – Examples
Example 1: Using an IF control statement IF (SELECT COUNT (*) FROM STUDENTS WHERE CLASS = :CLASS_NUM) > 30 THEN SET :MSG = 'Class is too large.'; ELSE SET :MSG = 'Class size is O.K.'; END IF;