Click or drag to resize

SA0186 : Possible missing BEGIN..END block

The topic describes the SA0186 analysis rule.

Message

Possible missing BEGIN..END block

Description

The rule checks IF/ELSE and WHILE statements, which do not have a BEGIN..END block, but the statements that follow it have the same indention or are placed on one line.

The omission of BEGIN...END block means that only the first following statement will be executed, which considering the same indention of the following statement might not be the expected behavior.

Review the reported statement for missed BEGIN..END or correct the indention if there was no intent to also include the following statements in the reported conditional statement.

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Categories

Design Rules, New Rules

Additional Information

There is no additional info for this rule.

Example Test Script
 1DECLARE @flag int = 0
 2IF (@flag=1)
 3  SELECT 1;
 4  SELECT 2;
 5SELECT 3;
 6
 7IF (@flag=1) SELECT 1; SELECT 2;
 8
 9IF (@flag=1) 
10SELECT 1; SELECT 2;
11
12IF (@flag=1) 
13SELECT 1; 
14SELECT 2;
15
16IF (@flag=1) SELECT 1;
17  SELECT 2;
18
19IF (@flag=1) SELECT 1;
20ELSE
21SELECT 1; SELECT 2;
22
23
24IF (@flag=1) 
25BEGIN
26  SELECT 1;
27  SELECT 2;
28END
29
30
31WHILE (@flag<5)
32  SELECT 1;
33  SELECT @flag = @flag + 1
34
35WHILE (@flag<10)
36  SELECT 1;
37SELECT @flag = @flag + 1;
38
39SELECT 3;

Analysis Results

 MessageLineColumn
1SA0186 : Possible misisng BEGIN..END block after IF statement.20
2SA0186 : Possible misisng BEGIN..END block after IF statement.70
3SA0186 : Possible misisng BEGIN..END block after IF statement.90
4SA0186 : Possible misisng BEGIN..END block after IF statement.120
5SA0186 : Possible misisng BEGIN..END block after IF statement.160
6SA0186 : Possible misisng BEGIN..END block after IF/ELSE clause.200
7SA0186 : Possible misisng BEGIN..END block after WHILE statement.310
See Also

Other Resources