Thursday, September 23, 2010

Using the IF.....ELSE Statement in SQL Server 2008.

You can use the IF…ELSE Statement for conditional execution of SQL statements. A particular action is performed when the given condition evaluates to TRUE and another action is performed when the given condition evaluates to FALSE.
     Other words, IF condition is satisfied and the Boolean expression returns TRUE, it will executed IF Block Sql statement.
IF condition is not satisfied and the Boolean expression returns FALSE, it will executed ELSE Block Sql Statement query.

Syntax for IF...ELSE statement.

IF (Boolean_expression)
BEGIN
Sql Statement Block
END
ELSE
BEGIN
Sql Statement Block
END

Example of IF...ELSE Statement.

IF (10>3)
   BEGIN
   SELECT 'Correct'
   END
ELSE
   BEGIN
   SELECT 'Wrong'
   END


IF (5<2)
   BEGIN
   SELECT 'Correct'
   END
ELSE
   BEGIN
   SELECT 'Wrong'
   END

The result is displayed below with IF….ELSE Statement.

No comments:

Recent Posts