IF Statement [T-SQL]

Provides conditional execution of a Transact-SQL statement, as an alternative to the Sybase IQ IF statement.

Syntax

 IF expression
... statement
... [ ELSEIF expression ] statement ]...

Examples

Usage

The Transact-SQL IF conditional and the ELSE conditional each control the performance of only a single SQL statement or compound statement (between the keywords BEGIN and END).

In contrast to the Sybase IQ IF statement, the Transact-SQL IF statement has no THEN. The Transact-SQL version also has no ELSEIF or END IF keywords.

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: You cannot nest the IF statement.

Standards

  • SQL—Transact-SQL extension to ISO/ANSI SQL grammar.

  • Sybase—Adaptive Server Enterprise supports the Transact-SQL IF statement.

Permissions

None