Here are some of the different flow control keywords used by SQL:

IF

When the condition is true, execute the next line, otherwise skip it.

IF BooleanExpression
    --Do lines here only if BooleanExpression=TRUE. Continue at 2nd comment.
--Always do this line.

IF...ELSE

When the condition is true, execute the next line and then jump to the second line after the ELSE statement. When the condition is false, jump to the line after the ELSE statement.

IF BooleanExpression
    --Do lines here only if BooleanExpression=TRUE. Continue at 3rd comment.
ELSE
   --Do lines here only if BooleanExpression=FALSE. Continue at 3rd comment.
   --Always do this line.

IF...ELSE statements can be chained together.

IF BooleanExpression1
    --Do lines here only if BooleanExpression1=TRUE. Continue at 2nd IF.
ELSE
    --Do lines here only if BooleanExpression1=FALSE. Continue at 2nd IF. 
IF BooleanExpression2     
    --Do lines here only if BooleanExpression2=TRUE. Continue at 4th comment.
--Always do this line.

BEGIN...END

Demarcates a block of Transact-SQL statements that can act as a single line. EG:

IF @Variable <> 0
BEGIN
   --Multiple statements go here
END

CASE

Note that CASE is different from the other flow control keywords in that CASE is actually used in SELECT statements as opposed to controlling when other statements will execute.

Returns a given expression when given another. 

CASE InputExpression
   WHEN WhenExpression THEN ResultExpression
   [...]
   [ ELSE ElseResultExpression ]
END
CASE
   WHEN BooleanExpression THEN ResultExpression
   [...]
   [ ELSE ElseResultExpression ]
END

CASE is often used as an on-the-fly look up table. EG:

SELECT
    CASE col1
        WHEN 'x' THEN 'xxx'
        WHEN 'y' THEN 'yyy'
        ELSE 'aaa'
    END AS "Column 1 Lookup",
    "Column 2 Lookup" =
    CASE
        WHEN SUBSTRING(tbl1.col2,1,2) = 'xx' THEN 'x'
        WHEN SUBSTRING(tbl1.col2,1,2) = 'yy' THEN 'y'
        ELSE 'a'
    END
FROM tbl1

Here is a friendlier example:

SELECT 
Name,
Party = CASE Name
    WHEN 'R' THEN 'Republican' 
    WHEN 'D' THEN 'Democrat'
    ELSE 'Other'
    END,
AgeGroup = CASE
    WHEN Age <  12  THEN 'Child'
    WHEN Age >= 12 AND < 20 THEN 'Teen'
    WHEN Age >=20 AND < 65 THEN 'Adult'
    WHEN Age >=65 THEN 'Senior'
    END
FROM Voter

GO

The GO command is not part of SQL but is used by SQL Server to indicate the end of a batch, i.e. a set of statements submitted for execution as a group. In contrast, a semicolon marks the end of a statement. The GO command also destroys any variables after it is run.

SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON
GO

-- The above GO command is needed because CREATE PROCEDURE must be the first statement of its batch

CREATE PROCEDURE MyStoredProcedure
AS
BEGIN
    ...
END

GOTO

Causes execution to continue at a specified label. EG:

IF @Variable = 3
GOTO hotel
   --Misc. statements here hotel:
   --Execution would continue here when the IF statement is FALSE

RETURN

Terminates execution.

TRY ... CATCH

This emulation of structured exception handling was introduced as of SQL Server 2005. Five functions were also introduced that start with ERROR_. ERROR_NUMBER() is an improvement over the old @@ERROR function. Both @@ERROR and RAISEERROR can still be used. This TRY ... CATCH example alsow shows a use of TRAN.

BEGIN TRAN;
BEGIN TRY
    --Next line might cause a constraint violation
    DELETE FROM Comment WHERE CommentID = 13;
    COMMIT TRAN;
END TRY
BEGIN CATCH
    ROLLBACK TRAN;
    SELECT
        ErrorLine = ERROR_LINE(), 
        ErrorMessage = ERROR_MESSAGE(), 
        ErrorNumber = ERROR_NUMBER(), 
        ErrorProcedure = ERROR_PROCEDURE(), 
        ErrorSeverity = ERROR_SEVERITY(), 
        ErrorState = ERROR_STATE();
END CATCH

WAITFOR DELAY

Specifies a delay period (up to 24 hours) before continuing.

WAITFOR TIME

Specifies a time to continue.

WHILE

WHILE BooleanExpression
    {SQLstatement | SQLStatementBlock}
NextLine

If the BooleanExpression is true, then go on to the next line (or block), and repeat it while the BooleanExpression is true with the following exceptions:

Page Modified: (Hand noted: 2007-10-12 21:40:39Z) (Auto noted: 2008-06-12 17:47:13Z)