SA0194 : The ELSE clause is not needed.If it is omitted the CASE expression will still return NULL as default value
Introduction
Section titled “Introduction”Unnecessary ELSE Clause in CASE Expressions in SQL statements can lead to redundant code and potential misunderstandings.
Description
Section titled “Description”In SQL Server and T-SQL programming, a CASE expression is used to evaluate conditions and return specific values when conditions are met. By default, if no WHEN condition is satisfied, the CASE expression returns a NULL value. Therefore, including an ELSE clause that explicitly returns NULL is redundant and should be avoided to enhance code readability and maintainability.
For example:
-- Example of a CASE expression with an unnecessary ELSE clauseSELECT CASE WHEN Condition1 THEN 'Result1' WHEN Condition2 THEN 'Result2' ELSE NULL END AS ResultColumnFROM SomeTable;In this example, the ELSE NULL part is unnecessary because SQL Server already defaults the result to NULL when no WHEN conditions are true.
-
The presence of
ELSE NULLadds to the complexity of the query without functional benefits. -
Removing the unnecessary
ELSE NULLclause clarifies the intended logic and simplifies the SQL statement.
How to fix
Section titled “How to fix”Remove unnecessary ELSE NULL clauses from CASE expressions in SQL statements to simplify and clarify the logic.
Follow these steps to address the issue:
1.Identify CASE expressions in your SQL queries where an ELSE NULL clause is present.
2.Remove the ELSE NULL clause from these expressions, as SQL Server automatically returns NULL when no WHEN conditions are met.
3.Review the SQL query to ensure the logic remains correct and clear without the redundant clause.
For example:
-- Original query with unnecessary ELSE NULLSELECT CASE WHEN Condition1 THEN 'Result1' WHEN Condition2 THEN 'Result2' ELSE NULL END AS ResultColumnFROM SomeTable;
-- Revised query with ELSE NULL removedSELECT CASE WHEN Condition1 THEN 'Result1' WHEN Condition2 THEN 'Result2' END AS ResultColumnFROM SomeTable;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”2 minutes per issue.
Categories
Section titled “Categories”Design Rules, Code Smells
Additional Information
Section titled “Additional Information”There is no additional info for this rule.
Example Test SQL
Section titled “Example Test SQL”Declare @x int = 5DECLARE @y SMALLINT = CONVERT(SMALLINT, RAND()*@x)
SELECT CASE @yWHEN 1 THEN 'a'WHEN 2 THEN 'b'ELSE NULLend
SELECT CASE CONVERT(SMALLINT, RAND()*@x)WHEN 1 THEN 'a'WHEN 2 THEN 'b'ELSE 'b'end
select CASEWHEN CONVERT(SMALLINT, RAND()*@x) = 1 THEN 'a'WHEN CONVERT(SMALLINT, RAND()*@x) = 2 THEN 'b'ELSE NULLENDAnalysis Results
Section titled “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 |