SA0023 : Avoid using not equal operator (<>,!=) in the WHERE clause

The topic describes the SA0023 analysis rule.

Message

Avoid using not equal operator (<>,!=) in the WHERE clause

Description

The rule checks for usage of the not equal operator in the WHERE clause as it result table and index scans.

Consider replacing the not equal operator with equals (=) or inequality operators (>,>=,<,<=) if possible.

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

Performance Rules, Code Smells

Additional Information
Example Test Script
SQL
 1CREATE INDEX IX_Table1_value ON Table1 (value)
 2
 3ALTER TABLE Table1 ADD computed_value AS (CASE WHEN value = 'value1' then 1 ELSE 0 END)
 4
 5CREATE INDEX IX_Table1_computed_value ON Table1(computed_value)
 6
 7-- Not equal operatror used in the WHERE clause.
 8SELECT * 
 9FROM  Table1 t1
10WHERE t1.value <> 'value1';
11
12-- Equal operatror used in the WHERE clause.
13SELECT * 
14FROM Table1 t1
15WHERE t1.computed_value = 0

Analysis Results

  Message Line Column
1 SA0023 : Avoid using not equal operator (<>,!=) in the WHERE clause. 10 15
See Also

Other Resources