IF Statement

Provides conditional execution of SQL statements.

Syntax

IF search-condition THEN statement-list
... [ ELSEIF search-condition THEN statement-list ]...
... [ ELSE statement-list ]
... END IF

Examples

Usage

The IF statement lets you conditionally execute the first list of SQL statements whose search-condition evaluates to TRUE.

If no search-condition evaluates to TRUE, and an ELSE clause exists, the statement-list in the ELSE clause is executed. If no search-condition evaluates to TRUE, and there is no ELSE clause, the expression returns a NULL value.

Execution resumes at the first statement after the END IF.

When comparing variables to the single value returned by a SELECT statement inside an IF statement, you must first assign the result of the SELECT to another variable.

Note: Do not confuse the syntax of the IF statement with that of the IF expression.

You cannot nest the IF statement.

For information on the IF expression, see Reference: Building Blocks, Tables, and Procedures > SQL Language Elements > Expressions.

Standards

  • SQL—ISO/ANSI SQL compliant.

  • Sybase—The Transact-SQL IF statement has a slightly different syntax.

Permissions

None

Related reference
BEGIN … END Statement