SA0002 : Variable declared but never referenced or assigned |
![]() |
The topic describes the SA0002 analysis rule.

Variable declared but never referenced or assigned

This rule checks for declared variables which are neither assigned nor used.
A variable is considered to be unused if it has been only declared, but never referenced in any following statement.

Remove the variable declaration.

The rule has a Batch scope and is applied only on the SQL script.

Name | Description | Default Value |
---|---|---|
IgnoreDefaultValueAssignment |
Parameter specifies whether the initialization of the variable to be considered assignment or not. |
no |

The rule does not need Analysis Context or SQL Connection.


Design Rules, Code Smells

There is no additional info for this rule.

SQL
1DECLARE @Title nvarchar(50) 2DECLARE @Title2 nvarchar(50) = 'Title2' 3DECLARE @Title3 nvarchar(50) 4DECLARE @Birthdate datetime 5 6SET @Title3= 'Title3' 7SET @Birthdate = '1979-01-11 00:00:00.000' 8 9SELECT * 10FROM Employee 11WHERE BirthDate > @Birthdate 12 13 14DECLARE @dialog_handle UNIQUEIDENTIFIER, 15 @ExpenseReport XML ; 16 17SET @ExpenseReport = '< construct message as appropriate for the application >' ; 18 19BEGIN DIALOG @dialog_handle 20FROM SERVICE [//Adventure-Works.com/Expenses/ExpenseClient] 21TO SERVICE '//Adventure-Works.com/Expenses' 22ON CONTRACT [//Adventure-Works.com/Expenses/ExpenseProcessing] ; 23 24 25BEGIN CONVERSATION TIMER (@dialog_handle) 26TIMEOUT = 120 ; 27 28SEND ON CONVERSATION @dialog_handle 29 MESSAGE TYPE [//Adventure-Works.com/Expenses/SubmitExpense] 30 (@ExpenseReport) ; 31 32DECLARE @ErrorSave INT 33 34SET @ErrorSave = @@ERROR; 35 36IF (@ErrorSave <> 0) 37BEGIN 38 39 DECLARE @ErrorDesc NVARCHAR(100); 40 DECLARE @ErrorDesc2 NVARCHAR(100); 41 42 DECLARE @ErrorDesc3 NVARCHAR(100); 43 44 SET @ErrorDesc = N'An error has occurred.'; 45 46 END CONVERSATION @dialog_handle 47 48 WITH ERROR = @ErrorSave DESCRIPTION = @ErrorDesc; 49 50 EXEC mysp_SaveErrorDesc @ErrorDesc2 = @ErrorDesc 51 52 DECLARE @result int 53 EXEC @result = mysp_SaveErrorDesc @ErrorDesc2 = @ErrorDesc3 54 55 DECLARE @Delay DateTime2 = '2013-01-01 00:00:00' 56 WAITFOR DELAY @Delay 57 58END |

Message | Line | Column | |
---|---|---|---|
1 | SA0002 : Variable @Title declared but never used. | 1 | 8 |
2 | SA0002 : Variable @Title2 declared but never used. | 2 | 8 |
3 | SA0002 : Variable @ErrorDesc2 declared but never used. | 40 | 12 |
