IF Statement

Lets you conditionally execute the first list of SQL statements whose search-condition evaluates to TRUE.

Quick Links:

Go to Examples

Go to Usage

Go to Standards

Go to Permissions

Syntax

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

Examples

(back to top)

Usage

(back to top)

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.

Standards

(back to top)

  • SQL—ISO/ANSI SQL compliant.
  • SAP Sybase Database product—The Transact-SQL IF statement has a slightly different syntax.

Permissions

(back to top)

None

Related reference
BEGIN … END Statement