MS SQL Server - Transact-SQL Topics: IF |
|
IF a Condition is True |
To find out whether a condition is true, you use the IF operator of Transact-SQL. Its basic formula is: IF Condition Statement When creating an IF statement, first make sure you provide a Condition expression that can be evaluated to produce true or false. To create this Condition, you can use variables and the logical comparison operator reviewed above. |
When the interpreter executes this statement, it first examines the Condition to evaluate it to a true result. If the Condition produces true, then the interpreter executes the Statement. Here is an example: DECLARE @DateHired As DateTime, @CurrentDate As DateTime SET @DateHired = '1996/10/04' SET @CurrentDate = '2007/04/11' IF @DateHired < @CurrentDate PRINT 'You have the experience required for a new promotion in this job' GO This would produce:
The IF condition we used above is appropriate when you only need to know if an expression is true. There is nothing to do in other alternatives. Consider the following code: DECLARE @DateHired As DateTime, @CurrentDate As DateTime SET @DateHired = '1996/10/04' SET @CurrentDate = '2007/04/16' IF @DateHired > @CurrentDate PRINT 'You have the experience required for a new promotion' GO This would produce: Notice that, in case the expression to examine produces a false result, there is nothing to do. Sometimes this will happen. |
|
||
Home | Copyright © 2007-2008 FunctionX, Inc. | |
|