Control-of-Flow Language

Control-of-flow language controls the flow of execution of SQL statements, statement blocks, and stored procedures. These keywords can be used in ad hoc SQL statements, in batches, and in stored procedures.

These are the control-of-flow keywords:

Keyword Description
BEGIN...END Defines a statement block.
GOTO label Continues processing at the statement following the label as defined by label.
IF...ELSE Defines conditional, and optionally, alternate execution when a condition is false.
RETURN Exits unconditionally.
WAITFOR Sets a delay for statement execution.
WHILE Repeats statements while a specific condition is true.
...BREAK Exits the innermost WHILE loop.
...CONTINUE Restarts a WHILE loop.

In addition to the keywords used for conditional processing and control-of-flow for SQL statements, a variety of helpful Transact-SQL extensions and ANSI-standard additions are available:

Feature Description
CASE Expression Allows an expression to have conditional return values. This is the ANSI SQL-92-standard CASE expression.
Comments Inserts a comment anywhere in an SQL statement. Two commenting styles are supported: Transact-SQL style (/* and */) and as ANSI-standard comment style (--).
DECLARE Statement Declares local variables as well as cursors.
PRINT Statement Prints a user-defined message on the user's screen.
RAISERROR Statement Returns a sysmessages entry or a dynamically built message with user-specified severity and state. RAISERROR also sets a system flag (in the global variable @@ERROR) to record that an error condition has occurred.

The control-of-flow keywords and their syntax are shown in the following sections.