SA0179 : Do not create function and procedures with too many parameters

Excessive parameters in SQL procedures and functions can signal potential design issues.

Description

When SQL procedures or functions have too many parameters, it often indicates that they are performing too many tasks or that there is a missed opportunity to structure the parameters more effectively, such as wrapping them in a composite type like a table or user-defined type.

For example:

SQL
 1-- Example of a problematic procedure with many parameters
 2CREATE PROCEDURE PerformMultipleTasks
 3    @Param1 INT,
 4    @Param2 INT,
 5    @Param3 INT,
 6    ...
 7    @ParamN INT
 8AS
 9BEGIN
10    -- Procedure logic
11END;

This can be problematic because managing a high number of parameters can:

  • Increase the complexity of procedure calls and maintenance.

  • Indicate a violation of the single-responsibility principle, making the procedure hard to optimize and debug.

  • Suggest that the use of a better data structure could simplify parameter management.

How to fix

Refactor procedures or functions with excessive parameters to improve design and maintainability.

Follow these steps to address the issue:

  1. Review the stored procedure or function parameters to understand their purpose and usage. Identify any parameters that can be grouped together or eliminated.

  2. Refactor the SQL procedure or function by considering combining related parameters into a single table or a user-defined type. This can help in reducing the number of parameters.

  3. Evaluate whether the procedure is performing multiple tasks. If so, consider splitting it into smaller procedures, each with a more focused role, complying with the single-responsibility principle.

For example:

SQL
 1-- Example of a refactored procedure using a user-defined table type
 2CREATE TYPE TaskParameters AS TABLE
 3(
 4    Param1 INT,
 5    Param2 INT,
 6    Param3 INT
 7);
 8
 9CREATE PROCEDURE PerformMultipleTasks
10    @TaskParams TaskParameters READONLY
11AS
12BEGIN
13    -- Procedure logic using parameters from @TaskParams
14END;

Scope

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

Parameters
Name Description Default Value
MaxNumberOfParameters

Maximum number of parameters.

10

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
1 hour per issue.
Categories

Design Rules, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE PROCEDURE TrestSA0179.ProcedureWithTooManyParameters
 2    @Param1 INT,  
 3    @Param2 INT,  
 4    @Param3 INT,  
 5    @Param4 INT,  
 6    @Param5 INT,  
 7    @Param6 INT,
 8    @Param7 INT,  
 9    @Param8 INT,  
10    @Param9 INT,  
11    @Param10 INT,  
12    @Param11 INT 
13AS  
14BEGIN  
15    SET NOCOUNT ON;  
16
17    SELECT @Param1,@Param2
18
19    RETURN 1;
20END

Analysis Results
  Message Line Column
1 SA0179 : The procedure has too many parameters. 1 0
See Also

Other Resources