Click or drag to resize

SA0002 : Variable declared but never referenced or assigned

The topic describes the SA0002 analysis rule.

Message

Variable declared but never referenced or assigned

Description

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.

Scope

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

Parameters
NameDescriptionDefault Value
IgnoreDefaultValueAssignment

Parameter specifies whether the initialization of the variable to be considered assignment or not.

no

Remarks

The rule does not need Analysis Context or SQL Connection.

Categories

Design Rules

Additional Information

There is no additional info for this rule.

Example Test Script
 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

Analysis Results

 MessageLineColumn
1SA0002 : Variable @Title declared but never used.18
2SA0002 : Variable @Title2 declared but never used.28
3SA0002 : Variable @ErrorDesc2 declared but never used.4012
See Also

Other Resources