IF - ELSE is the most commonly used Transact SQL command, that executes a statement depending on the condition. If the condition for IF statement is True, the statements inside the 'Begin-End' loop of 'IF' statement will be executed. If the condition for IF statement is False, the statements inside the 'Begin-End' loop of 'ELSE' statement will be executed. Thus, depending on which condition is true, statement or block of statements are executed
The basic Syntax for IF..ELSE is:
IF ( Boolean_expression )
BEGIN
'Sql Statement Block'
END
ELSE
BEGIN
'Sql Statement Block'
END
Simple If..Else Statement example
IF (x>y)BEGIN
SELECT 'If x is greater than y, this statement will be executed'
END
ELSE
BEGIN
SELECT 'If y is greater than x, this statement will be executed'
END
Nested If..Else Statement example
IF (x>y)BEGIN
SELECT 'If x is greater than y, this statement will be executed'
IF (x>z)
BEGIN
SELECT 'If x is greater than y and z, this statement will be executed'
END
END
ELSE
BEGIN
SELECT 'If y is greater than x, this statement will be executed'
END
Comments
Post a Comment