SA0182 : The CASE expressions is missing ELSE clause

The topic describes the SA0182 analysis rule.

Message

The CASE expressions is missing ELSE clause

Description

The rule reports CASE expressions, which are missing ELSE clause.

In terms of defensive programming the ELSE clause should either have a default action or contain a comment explaining the reason an action is not taken.

How to fix

Add an ELSE clause to the CASE expression with either a default action, or a comment explaining the reason an action is not taken.

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, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1Declare @x int = 5
 2DECLARE @y int = CONVERT(int, RAND()*@x) 
 3
 4select CASE @x
 5WHEN 1 THEN 'a'
 6WHEN 2 THEN 'b'
 7ELSE 'c'
 8end
 9
10select CASE CONVERT(int, RAND()*@y)
11WHEN 1 THEN 'a'
12WHEN 2 THEN 'b'
13end

Analysis Results
  Message Line Column
1 SA0182 : The CASE expressions is missing ELSE clause. 10 7
See Also

Other Resources