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.

How to fix

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.

Effort To Fix
5 minutes per issue.
Categories

Design Rules, Bugs

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 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
  Message Line Column
1 SA0186 : Possible misisng BEGIN..END block after IF statement. 2 0
2 SA0186 : Possible misisng BEGIN..END block after IF statement. 7 0
3 SA0186 : Possible misisng BEGIN..END block after IF statement. 9 0
4 SA0186 : Possible misisng BEGIN..END block after IF statement. 12 0
5 SA0186 : Possible misisng BEGIN..END block after IF statement. 16 0
6 SA0186 : Possible misisng BEGIN..END block after IF/ELSE clause. 20 0
7 SA0186 : Possible misisng BEGIN..END block after WHILE statement. 31 0
See Also

Other Resources