SA0039 : The comparison expression evaluates to FALSE
Introduction
Section titled “Introduction”Misconfigured comparison expressions that consistently evaluate to FALSE in SQL queries can uncover logical errors in a SQL query.
Description
Section titled “Description”This problem occurs when SQL queries contain comparison expressions that are logically incorrect, causing them to always return FALSE . Such expressions can lead to inefficient queries, unnecessary resource consumption, and incorrect query results.
For example:
-- Example of a problematic querySELECT * FROM Employees WHERE EmployeeStatus > EmployeeStatus;In this example, the condition EmployeeStatus > EmployeeStatus will never be true, resulting in an empty result set and wasted execution resources. Misconfigured comparisons like this are common pitfalls that can occur due to logical errors or incorrect assumptions about data.
-
Queries with such conditions are inefficient because they always return the same static result.
-
Logical mistakes in query design can lead to data integrity issues and incorrect business insights.
How to fix
Section titled “How to fix”Review and correct logically misconfigured comparison expressions in SQL queries to ensure they don’t always evaluate to FALSE, thereby improving query efficiency and accuracy.
Follow these steps to address the issue:
1.Identify the problematic comparison expression. Look for conditions in your SQL queries that always evaluate to FALSE, such as 1 = 0 or 'A' = 'B' , or A != A .
2.Determine the logical intent of the condition. Understand what the condition was meant to achieve by reviewing the business logic or data requirements.
3.Revise the condition to correctly reflect the intended logic. Ensure that the expression can evaluate to TRUE under the correct circumstances.
For example:
-- Corrected example of a querySELECT * FROM Employees WHERE EmployeeStatus = 'Active';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”3 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”-- Comparison will always evaluate to FALSE.SELECT *FROM sys.objects AS oWHERE 1 != 1 OR 1 > 1 OR 0 > 1 OR 1 = 0 OR 0 != 0 OR 2 != 2 OR 2 <> 2 OR 2 < 1 OR 2 <= 1 OR 2 <> 2.0 OR 2.0 < 1.0 OR 2 <= 1 OR 1 >= 2 OR object_id != [object_id] OR o.object_id != o.[object_id] OR 'A' < 'A' OR 'A' != 'A' OR 'B' <> 'B' OR 'B' <> N'B' OR 'ab' != 'ab' OR 'ab' != N'ab' OR 'ab' < N'a' OR 'ab' < N'a' OR 'ab' = N'a' OR 'ab' < 'a' OR 'ab' < 'a' OR 'ab' = 'a' OR name != name OR name <> name OR name < name OR o.[name] > o.name OR name NOT LIKE name OR 1 IN( 2, 4, 3 ) OR 'a' IN( 'b', 'c', 'd', 6 ) OR 1 NOT IN( 1, 2, 4, 3, 2 - 1 ) OR 1 IN( 2, 4, 3 ) OR 'a' NOT IN( 'b', 'c', 'd', 'a' ) OR NOT EXISTS( SELECT 0 )Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0039 : The comparison expression evaluates to FALSE. | 35 | 16 |
| 2 | SA0039 : The comparison expression evaluates to FALSE. | 36 | 9 |
| 3 | SA0039 : The comparison expression evaluates to FALSE. | 37 | 11 |
| 4 | SA0039 : The comparison expression evaluates to FALSE. | 38 | 13 |
| 5 | SA0039 : The comparison expression evaluates to FALSE. | 39 | 9 |
| 6 | SA0039 : The comparison expression evaluates to FALSE. | 40 | 15 |
| 7 | SA0039 : The comparison expression evaluates to FALSE. | 41 | 11 |
| 8 | SA0039 : The comparison expression evaluates to FALSE. | 4 | 9 |
| 9 | SA0039 : The comparison expression evaluates to FALSE. | 5 | 9 |
| 10 | SA0039 : The comparison expression evaluates to FALSE. | 6 | 9 |
| … | |||
| 37 | SA0039 : The comparison expression evaluates to FALSE. | 33 | 12 |
| 38 | SA0039 : The comparison expression evaluates to FALSE. | 34 | 16 |