Tutorials
Search…
Lesson 31. Controlling Flow
In the next two lessons, we are going to learn about IF..ELSE and WHILE. These are standard control flow elements that you will find in any programming language. However, I personally rarely find a use for them in a business intelligence context.
But they are major parts of the T-SQL programming language, so, just in case, here is how to control program execution with IF..ELSE.

IF ELSE Syntax

In [ ]:
IF <logic condition>
BEGIN
--Do some action.
END
ELSE
BEGIN
--Do some alternative action.
END

Examples

A Basic IF ELSE Example
In [ ]:
USE demo
DECLARE @i TINYINT = 1
IF @i = 1
BEGIN
PRINT 'The variable value is 1.'
END
ELSE
BEGIN
PRINT 'The variable value is not 1.'
END
Copy link
On this page
IF ELSE Syntax
Examples