Home
Non-standard comparison operators should not be used
Description
The rule "Non-standard comparison operators should not be used" states that any comparison operators other than the standard ones provided by SQL Server should not be used. This includes operators such as <, >, =, !=, <>, etc. These operators should only be used for comparing values in the same type, and not for comparing values between different types. This rule is important for ensuring that queries are properly formed and that data is accurately compared.
Key Benefits
- Clarity: Using standard comparison operators ensures code is easily readable and understandable.
- Maintainability: Non-standard comparison operators can be confusing and difficult to maintain.
- Safety: Using standard comparison operators helps to reduce the chances of introducing bugs into the code.
Non-compliant Code Example
DECLARE @Number int; SET @Number = 50; IF @Number !> 10 --Non compliant code (Non-standard comparison operators is used) BEGIN Select * From Employee Where id < @Number; PRINT 'The number is less then 10.'; END ELSE IF @Number !< 100 --Non compliant code (Non-standard comparison operators is used) BEGIN Select * From Employee Where id < @Number; PRINT 'The number is greater then 100.'; END ELSE PRINT 'The number is medium.'; GO
Compliant Code Example
DECLARE @Number int; SET @Number = 50; IF @Number < 10 --Compliant code (Standard comparison operators is used) BEGIN Select * From Employee Where id < @Number; PRINT 'The number is less then 10.'; END ELSE IF @Number > 100 --Compliant code (Standard comparison operators is used) BEGIN Select * From Employee Where id < @Number; PRINT 'The number is greater then 100.'; END ELSE PRINT 'The number is medium.'; GO