Programming in MS SQL Server 2012 |
IF StatementIF statement is a conditional SQL Server statement that return Boolean TRUE or FALSE result value based on defined expression. IF ... ELSE ... END statements are uses in user-defined function, stored procedure and ad-hoc queries. IF condition is TRUE, SQL Server execute nested to IF statements or execute nested to ELSE statements. Syntax -- Syntax --
IF Boolean_expression { sql_statement | statement_block } [ ELSE { sql_statement | statement_block } ] Boolean_expression Boolean_expression is an expression that returns TRUE or FALSE. If the Boolean expression contains a SELECT statement, the SELECT statement must be enclosed in parentheses. Sql_statement | statement_block Sql_statement | statement_block is any Transact-SQL statement or statement grouping that defined with BEGIN ... END block. The following example describes IF ... ELSE ... END statement. -- Example 84 --
DECLARE @ProductExpiryDate DateTime, @ServerSystemDate DateTime
SET @ProductExpiryDate = '12/31/2005' SET @ServerSystemDate = GETDATE()
IF @ProductExpiryDate < @ServerSystemDate PRINT 'TRUE' ELSE PRINT 'FALSE' Query Output Screen |
* * * * *