Programming in MS SQL Server 2012

IF Statement

IF 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 --


IF Boolean_expression

     { sql_statement | statement_block }


     { sql_statement | statement_block } ]

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




Query Output Screen

* * * * *

Email Your Comment To AUTHOR