SA0029 : Input parameter never used
Introduction
Section titled “Introduction”Unused parameters in stored procedures or functions can lead to confusion, reduced code readability, and maintenance challenges.
Description
Section titled “Description”In SQL Server, stored procedures and functions can have parameters that may not be utilized within the body of their respective code blocks. When parameters are declared but not used, it results in unnecessary complexity within the database schema. This issue can lead to confusion, increased maintenance efforts, and it detracts from code readability.
For example:
-- Example of a stored procedure with unused parametersCREATE PROCEDURE GetEmployeeDetails @EmployeeID INT, @DepartmentID INTASBEGIN SELECT Name, Position FROM Employees WHERE EmployeeID = @EmployeeID;END;In this example, @DepartmentID is declared but never used. Such unused parameters do not directly affect performance but add unwanted clutter, misleading developers and making future code modifications cumbersome.
-
Unused parameters increase the complexity of stored procedures or functions without any functional benefit.
-
They may cause confusion about the procedure’s or function’s actual input requirements and make future maintenance trickier.
How to fix
Section titled “How to fix”Remove unused input parameters from stored procedures or functions to optimize code clarity and maintainability.
Follow these steps to address the issue:
1.Analyze the stored procedure or function to identify parameters that are declared but not used within the code block. Use sp_helptext or inspect the procedure directly in SQL Server Management Studio (SSMS).
2.Edit the stored procedure or function to remove the unused parameters. In SSMS, right-click the procedure and select Modify to open the SQL script for editing.
3.Delete the unused parameter from the procedure’s parameter list and any line of code where it might have been initialized or commented out.
4.Save and recompile the procedure/function by executing the ALTER statement to ensure that your changes take effect.
5.Test the modified procedure/function to ensure it still functions as expected without the unused parameter.
For example:
-- Original stored procedure with an unused parameterCREATE PROCEDURE GetEmployeeDetails @EmployeeID INT, @DepartmentID INTASBEGIN SELECT Name, Position FROM Employees WHERE EmployeeID = @EmployeeID;END;
-- Modified procedure without the unused parameterALTER PROCEDURE GetEmployeeDetails @EmployeeID INTASBEGIN SELECT Name, Position FROM Employees WHERE EmployeeID = @EmployeeID;END;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”2 minutes per issue.
Categories
Section titled “Categories”Design Rules, Code Smells
Additional Information
Section titled “Additional Information”There is no additional info for this rule.
Example Test SQL
Section titled “Example Test SQL”CREATE PROCEDURE Production.uspGetList @Product varchar(40) , @VendorCode varchar(20) -- parameter is not used , @VendorCode2 varchar(20) -- parameter is not used, but the rule is suppressed because this comment contains 'IGNORE:SA0029' text. , @MaxPrice money , @Input int -- parameter is only assigned, but not usedAS SET NOCOUNT ON;
SET @Input = 0 SELECT p.[Name] AS Product, p.ListPrice AS 'List Price' FROM Production.Product AS p WHERE s.[Name] LIKE @Product AND p.ListPrice < @MaxPrice;Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0029 : The input parameter @VendorCode never used. | 3 | 3 |
| 2 | SA0029 : The input parameter @Input never used. | 6 | 6 |