Click or drag to resize

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.

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 @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

 MessageLineColumn
1SA0194 : The ELSE clause is not needed.If it is omitted the CASE expression will still return NULL as default value.70
2SA0194 : The ELSE clause is not needed.If it is omitted the CASE expression will still return NULL as default value.190
See Also

Other Resources