SR0013: Output parameter (parameter) is not populated in all code paths
RuleId |
SR0013 |
Category |
Microsoft.Design |
Breaking Change |
Not Breaking |
Cause
Your output parameters are not initialized in all possible code paths throughout a stored procedure or function.
Rule Description
This rule identifies code in which the output parameter is not set to a value in one or more code paths through the stored procedure or function. This rule does not identify in which paths the output parameter should be set. If multiple output parameters have this problem, one warning appears for each parameter.
How to Fix Violations
You can correct this issue in one of two ways. You can fix this issue most easily if you initialize the output parameters to a default value at the start of the procedure body. As an alternative, you can also set the output parameter to a value in the specific code paths in which the parameter is not set. However, you might overlook an uncommon code path in a complex procedure.
Important
Specifying a value within the procedure declaration, such as CREATE PROC MyProcedure (@param1 INT = 10 OUTPUT) will not resolve the issue. You must assign a value to the output parameter within the procedure body.
When to Suppress Warnings
You should not suppress this warning. As a best practice, you should always set your output parameters to a value. If an indeterminate value is returned, unexpected results might adversely affect the code that calls the stored procedure or function.
Example
The following example shows two simple procedures. The first procedure does not set the value of the output parameter, @Sum. The second procedure initializes the @Sum parameter at the start of the procedure, which ensures that the value will be set in all code paths.
CREATE PROCEDURE [dbo].[procedureHasWarning]
(
@Value1 BIGINT,
@Value2 INT,
@Value3 INT,
@Sum INT OUTPUT
)
AS
BEGIN
-- No initialization of the output parameter
--
-- Additional statements here.
--
RETURN 0;
END
--
CREATE PROCEDURE [dbo].[procedureFixed]
(
@Value1 BIGINT,
@Value2 INT,
@Value3 INT,
@Sum INT OUTPUT
)
AS
BEGIN
-- Initialize the out parameter
SET @Sum = 0;
--
-- Additional statements here
--
RETURN 0;
END