Provides conditional execution of a Transact-SQL statement, as an alternative to the SAP Sybase IQ IF statement.
IF expression ... statement ... [ ELSE [ IF expression ] statement ]...
IF (SELECT max(id) FROM sysobjects) < 100 RETURN ELSE BEGIN PRINT 'These are the user-created objects' SELECT name, type, id FROM sysobjects WHERE id < 100 END
BEGIN DECLARE @X INT SET @X = 1 IF @X = 1 PRINT '1' ELSEIF @X = 2 PRINT '2' ELSE PRINT 'something else' END
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 SAP 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.