Control-of-Flow Statements

We've actually covered a great deal of the control-of-flow language already. The WHILE and GOTO statements are forms of control-of-flow statements, which modify the order in which SQL statements are executed. The last main control-of-flow statements available to you are IF...ELSE and CASE.

IF...ELSE

The IF...ELSE statement block is used to make a decision based on supplied parameters and to perform some action based on that decision. For example, if a variable contains a specific value, SQL Server will perform some action. Otherwise, that action won't be taken. The syntax for the IF...ELSE block is as follows:

IF expression
BEGIN
    sql_statements
END
ELSE
BEGIN
    sql_statements
END

The options for this statement ...

Get Writing Stored Procedures for Microsoft SQL Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.