Control Structures

IF <expression> THEN
Executable_statements
ELSE
Executable_Statements

If executable statements are more than one statement then use BEGIN … END block for grouping the statements.

Recommendation:

  1. One line space after executable statements
  2. Executable statements should be indented atleast by 3  spaces.
  3. If the executable statements inside an IF statement clause themselves contain another IF statement, then IF- ELSE and keywords should be indented inside the enclosing IF statements for the inner IF should be indented further so that they are clearly with in the nested IF.

Formatting Loops

Transact-SQL offers the WHILE loop. The GOTO statement can also be used for looping purposes.

Syntax:

WHILE Boolean_expression
{sql_statement | statement_block}
[BREAK] [CONTINUE]

Eg:
WHILE condition
Executable_Statements
END

BREAK / CONTINUE can be used within the loop to encounter the exceptions.

Each loop has a loop boundary and a loop body. The loop body should be indented from the boundary.