SA0194 : The ELSE clause is not needed.If it is omitted the CASE expression will still return NULL as default value

The topic describes the SA0194 analysis rule.

Message

The ELSE clause is not needed.If it is omitted the CASE expression will still return NULL as default value

Description

The rule reports CASE expressions, which have an ELSE clause, which returns NULL value.

If none of the WHEN conditions equals TRUE, the CASE expression will return NULL by default.

In this case the ELSE clause, which returns NULL is not needed and cloud be removed.

How to fix

Remove the redundant ELSE clause.

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
2 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 SMALLINT = CONVERT(SMALLINT, RAND()*@x) 
 3
 4SELECT CASE @y
 5WHEN 1 THEN 'a'
 6WHEN 2 THEN 'b'
 7ELSE NULL
 8end
 9
10SELECT CASE CONVERT(SMALLINT, RAND()*@x)
11WHEN 1 THEN 'a'
12WHEN 2 THEN 'b'
13ELSE 'b'
14end
15
16select CASE   
17WHEN CONVERT(SMALLINT, RAND()*@x)  = 1 THEN 'a'  
18WHEN CONVERT(SMALLINT, RAND()*@x)  = 2 THEN 'b'
19ELSE NULL
20END

Analysis Results
  Message Line Column
1 SA0194 : The ELSE clause is not needed.If it is omitted the CASE expression will still return NULL as default value. 7 0
2 SA0194 : The ELSE clause is not needed.If it is omitted the CASE expression will still return NULL as default value. 19 0
See Also

Other Resources