SQL-Query Basics  «Prev  Next»
Lesson 5 Logic within queries
Objective Practice writing logic within your queries.

Logic within Queries of SQL-Server 2012

Many times in your SQL procedures you need to test the value of a variable, then take some action based on that value.
You can do this with SQL Server’s Transact-SQL control-of-flow logic. Control-of-flow logic is a specific set of Transact-SQL commands that you can enter into your Transact-SQL procedures to dynamically control which statements are executed.

The following is a list of the keywords available in Transact-SQL:
  1. BEGIN…END – Defines a block of Transact-SQL code
  2. GOTO – Instructs the processor to go to a specific label defined in the procedure
  3. IF…ELSE – Tests for conditions that you specify. The results of these conditions always return values of TRUE or FALSE. As such, they are known as Boolean expressions.
  4. RETURN – Exits the procedure
  5. WAITFOR – Delays the execution of a statement until specific conditions exist
  6. WHILE – Loops until a specific condition exists
  7. BREAK – Unconditionally exits a WHILE loop
  8. CONTINUE – Restarts the WHILE loop
  9. CASE…WHEN – Tests multiple possibilities for a given condition
The Transact-SQL statement in the following series of images uses most of the keywords listed above:

@LoopCount is declared as an integer variable that will be used in the rest of the statement
1) @LoopCount is declared as an integer variable that will be used in the rest of the statement

@LoopCount is initialized to a value of 0
2) @LoopCount is initialized to a value of 0

The WHILE loop is set up to be active as long as the @LoopCount variable is less than 10.
3) The WHILE loop is set up to be active as long as the @LoopCount variable is less than 10.

The BEGIN and END statement start and stop the loop
4) The BEGIN and END statement start and stop the loop

The @LoopCount variable is incremented by one, then tested. When @LoopCount equals 5,
 the BREAK statement is executed.
5) The @LoopCount variable is incremented by one, then tested. When @LoopCount equals 5, the BREAK statement is executed.

When the BREAK statement is executed, the value of @LoopCount will be tested again. Note that no BEGIN ... END construct is needed around the BREAK statement because it is a single statement
6) When the BREAK statement is executed, the value of @LoopCount will be tested again. Note that no BEGIN ... END construct is needed around the BREAK statement because it is a single statement

If @LoopCount = 5, a new loop is entered.
7) If @LoopCount = 5, a new loop is entered.

A static string is constructed and returned back to the client.
8) A static string is constructed and returned back to the client.

SQL Server is instructed to jump immediately to the label Five_label.
9) SQL Server is instructed to jump immediately to the label Five_label.

If @LoopCount did not equal 5.
10) If @LoopCount did not equal 5.

The else condition returns a static string back to the client.
11) The else condition returns a static string back to the client.

The value unconditionally exits from the entire procedure.
12) The value unconditionally exits from the entire procedure.

Placeholder: need to get correct text for this page
13) Placeholder: need to get correct text for this page

A string is constructed and returned back to the client, indicating that the code make it into the label.
14) A string is constructed and returned back to the client, indicating that the code make it into the label.

Using While Statement in SQL-Server

This SQL Server tutorial explains how to use the WHILE LOOP in SQL Server (Transact-SQL) with syntax and examples. In SQL Server, you use a WHILE LOOP when you are not sure how many times you will execute the loop body and the loop body may not execute even once.

WHILE condition
BEGIN
{...statements...}
END;

Parameters or Arguments

  1. condition: The condition is test each pass through the loop. If condition evaluates to TRUE, the loop body is executed. If condition evaluates to FALSE, the loop is terminated.
  2. statements: The statements of code to execute each pass through the loop.
Pay attention to the following facts:
  1. You would use a WHILE LOOP statement when you are unsure of how many times you want the loop body to execute.
  2. Since the WHILE condition is evaluated before entering the loop, it is possible that the loop body may not execute even once.
  3. See also the BREAK statement to exit from the WHILE LOOP early.
  4. See also the CONTINUE statement to restart the WHILE LOOP from the beginning.

Note that the SELECT statement is covered in a later lesson in this module. The main point of this lesson is to show the structure of how to use logic in your queries. The actual SELECT statements shown are not significant. In the next lesson, the benefits of constructing SQL statements dynamically will be discussed.

Logic with Queries - Exercise

Before moving on to the next lesson, click the Exercise link below to practice writing query logic.
Logic within Queries - Exercise