If...Else statements allow you to add some conditional logic to your SQL queries or scripts. The lovely MSDN syntax for IF...ELSE statements in SQL Server 2008 R2 looks like this:
- IF Boolean_expression { sql_statement | statement_block }
- [ ELSE { sql_statement | statement_block } ]
Boolean_expression What is meant by the Boolean_expression? A boolean expression is any expression that returns either TRUE or FALSE as its result. When using IF...ELSE statements in SQL Server 2008 R2, if the boolean expression contains a SELECT statement, the SELECT statement must be enclosed in parentheses. If the expression evaluates to TRUE, the sql_statement or statement_block will execute, otherwise if it is FALSE, the sql_statement or statement_block in the ELSE clause will execute (if available).
sql_statement | statement_block What is meant by sql_statement | statement_block? Well, an sql_statement is any valid single SQL statement, and a statement_block is a group of valid SQL statements within a BEGIN...END block.
A simple example. Here is a fairly basic example of an IF...ELSE statement in SQL Server:
- IF 1 = 1 PRINT 'Boolean_expression is true.'
- ELSE PRINT 'Boolean_expression is false.' ;
What the above query is doing is checking if 1 is equal to 1. And it is. So the statement returns TRUE and then 'Boolean_expression is true.' is printed to the screen. If we changed the boolean expression to '1 = 2', it would return FALSE and 'Boolean_expression is false.' would be printed to the screen.
- IF 1 = 2 PRINT 'Boolean_expression is true.'
- ELSE PRINT 'Boolean_expression is false.' ;
Here is an example using the Adventure Works database
- USE AdventureWorks;
- GO
- DECLARE @AvgWeight decimal(8,2), @BikeCount int
- IF
- (SELECT COUNT(*) FROM Production.Product WHERE Name LIKE 'Touring-3000%' ) > 5
- BEGIN
- SET @BikeCount =
- (SELECT COUNT(*)
- FROM Production.Product
- WHERE Name LIKE 'Touring-3000%');
- SET @AvgWeight =
- (SELECT AVG(Weight)
- FROM Production.Product
- WHERE Name LIKE 'Touring-3000%');
- PRINT 'There are ' + CAST(@BikeCount AS varchar(3)) + ' Touring-3000 bikes.'
- PRINT 'The average weight of the top 5 Touring-3000 bikes is ' + CAST(@AvgWeight AS varchar(8)) + '.';
- END
- ELSE
- BEGIN
- SET @AvgWeight =
- (SELECT AVG(Weight)
- FROM Production.Product
- WHERE Name LIKE 'Touring-3000%' );
- PRINT 'Average weight of the Touring-3000 bikes is ' + CAST(@AvgWeight AS varchar(8)) + '.' ;
- END ;
- GO
CASE Statements
There are two types of CASE statements in SQL Server: simple CASE expression and searched CASE expression.
Simple CASE Expression
The simple CASE expression works by comparing the first expression to the expression in each WHEN clause, to see if they are equal. If they are,then the expression in the THEN clause is returned.
Here is an example of the simple CASE expression:
- USE AdventureWorks;
- GO
- SELECT ProductNumber, Category =
- CASE ProductLine
- WHEN 'R' THEN 'Road'
- WHEN 'M' THEN 'Mountain'
- WHEN 'T' THEN 'Touring'
- WHEN 'S' THEN 'Other sale items'
- ELSE 'Not for sale'
- END,
- Name
- FROM Production.Product
- ORDER BY ProductNumber;
- GO
What the above query is doing is selecting the category, which is stored as a single letter, such as "R", and then determining which category it is, and returning a category name which is easier to understand.
Searched CASE Expression
The Searched CASE Expression checks the boolean expressions in the order they are typed. It returns whichever one is evaluated to TRUE first. If none of them return TRUE, the searched CASE statement returns whatever is in the ELSE clause, or NULL if there is no ELSE clause specified.
- USE AdventureWorks;
- GO
- SELECT ProductNumber, Name, 'Price Range' =
- CASE
- WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
- WHEN ListPrice < 50 THEN 'Under $50'
- WHEN ListPrice >= 50 AND ListPrice < 250 THEN 'Greater than, or equal to, $50 but Under $250'
- WHEN ListPrice >= 250 AND ListPrice < 1000 THEN 'Greater than, or equal to, $250 but Under $1000'
- ELSE 'Over $1000'
- END
- FROM Production.Product
- ORDER BY ProductNumber ;
- GO
What the above query will do is check the price of a product, based on the ProductNumber, and then assign a text comment based on the price of that product if it falls within a certain numeric range.