Home
Variables should not be self-assigned
Description
The rule "Variables should not be self-assigned" states that variables should not be assigned a value that is equal to its own existing value. This rule is important when writing SQL Server code, as assigning a variable to itself can lead to unexpected results. Self-assigning variables can also lead to poor performance, as the server will need to evaluate and assign the same value multiple times. In order to ensure optimal performance and accuracy, variables should only be assigned values that differ from their existing value.
Key Benefits
- Better readability : Variables should not be self-assigned as it is difficult to read and understand code that assigns a variable to itself.
- Easier debugging : Self-assignment of variables can lead to unexpected results and can be difficult to debug.
- Consistent code : Self-assigning variables can lead to inconsistent code that can be difficult to maintain.
Non-compliant Code Example
DECLARE @Number INT = 100;;
SET @Number = @Number --Non compliant code (Variable is self assigned)