Summary
: in this tutorial, you will learn SQL Server
IF...ELSE
statement to control the flow of program.
The
IF...ELSE
statement is a control-flow statement that allows you to execute or skip a
statement block
based on a specified condition.
The
IF
statement
The following illustrates the syntax of the
IF
statement:
IF boolean_expression
BEGIN
{ statement_block }
END
Code language: SQL (Structured Query Language) (sql)
In this syntax, if the
Boolean_expression
evaluates to
TRUE
then the
statement_block
in the
BEGIN...END
block is executed. Otherwise, the
statement_block
is skipped and the control of the program is passed to the statement after the
END
keyword.
Note that if the Boolean expression contains a
SELECT
statement, you must enclose the
SELECT
statement in parentheses.
The following example first gets the sales amount from the
sales.order_items
table in the
sample database
and then prints out a message if the sales amount is greater than 1 million.
BEGIN
DECLARE @sales INT;
SELECT
@sales = SUM(list_price * quantity)
sales.order_items i
INNER JOIN sales.orders o ON o.order_id = i.order_id
WHERE
YEAR(order_date) = 2018;
SELECT @sales;
IF @sales > 1000000
BEGIN
PRINT 'Great! The sales amount in 2018 is greater than 1,000,000';
Code language: SQL (Structured Query Language) (sql)
The output of the code block is:
Great! The sales amount in 2018 is greater than 1,000,000
Code language: SQL (Structured Query Language) (sql)
Note that you have to click the Messages tab to see the above output message:

The
IF ELSE
statement
When the condition in the
IF
clause evaluates to
FALSE
and you want to execute another statement block, you can use the
ELSE
clause.
The following illustrates the
IF ELSE
statement:
IF Boolean_expression
BEGIN
-- Statement block executes when the Boolean expression is TRUE
BEGIN
-- Statement block executes when the Boolean expression is FALSE
Code language: SQL (Structured Query Language) (sql)
Each
IF
statement has a condition. If the condition evaluates to
TRUE
then the statement block in the
IF
clause is executed. If the condition is
FALSE
, then the code block in the
ELSE
clause is executed.
See the following example:
BEGIN
DECLARE @sales INT;
SELECT
@sales = SUM(list_price * quantity)
sales.order_items i
INNER JOIN sales.orders o ON o.order_id = i.order_id
WHERE
YEAR(order_date) = 2017;
SELECT @sales;
IF @sales > 10000000
BEGIN
PRINT 'Great! The sales amount in 2018 is greater than 10,000,000';
BEGIN
PRINT 'Sales amount in 2017 did not reach 10,000,000';
Code language: SQL (Structured Query Language) (sql)
In this example:
First, the following statement sets the total sales in 2017 to the
@sales
variable:
SELECT
@sales = SUM(list_price * quantity)
sales.order_items i
INNER JOIN sales.orders o ON o.order_id = i.order_id
WHERE
YEAR(order_date) = 2017;
Code language: SQL (Structured Query Language) (sql)
Second, this statement returns the sales to the output:
SELECT @sales;
Code language: SQL (Structured Query Language) (sql)
Finally, the
IF
clause checks if the sales amount in 2017 is greater than 10 million. Because the sales amount is less than that, the statement block in the
ELSE
clause executes.
IF @sales > 10000000
BEGIN
PRINT 'Great! The sales amount in 2018 is greater than 10,000,000';
BEGIN
PRINT 'Sales amount in 2017 did not reach 10,000,000';
END
Code language: SQL (Structured Query Language) (sql)
The following shows the output:
Sales amount did not reach 10,000,000
Code language: SQL (Structured Query Language) (sql)
Nested
IF...ELSE
SQL Server allows you to nest an
IF...ELSE
statement within inside another
IF...ELSE
statement, see the following example:
BEGIN
DECLARE @x INT = 10,
@y INT = 20;
IF (@x > 0)
BEGIN
IF (@x < @y)
PRINT 'x > 0 and x < y';