SQL Server Error 591: Fix Output Parameter Issues
In this article
SQL Server Error 591 occurs when you attempt to execute a stored procedure and pass a value to a parameter that is defined as an OUTPUT parameter, but you fail to include the OUTPUT keyword in the EXECUTE statement. To fix this, you must add the OUTPUT keyword immediately after the variable or value being passed to that specific parameter in your call statement.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or a compatible SQL editor.
- Permissions to execute the stored procedure in question.
- The definition of the stored procedure to identify which parameters are marked as
OUTPUT. - A test environment or development database to verify the fix before deploying to production.
Step-by-Step
- Identify the Problematic Parameter: First, examine the stored procedure definition. Right-click the procedure in the Object Explorer and select
Modify. Look for parameters defined with theOUTPUTkeyword. For example, if you see@UserCount INT OUTPUT, this parameter is designed to send a value back to the calling program. - Analyze the Execution Call: Look at the T-SQL code you are using to call the procedure. A typical failing call looks like this:
EXEC usp_GetUserCount @DepartmentID = 10, @Count = @MyVariable. In this scenario, if@Countis an output parameter, the missingOUTPUTkeyword triggers Error 591. - Declare a Local Variable: Before calling the procedure, ensure you have declared a variable that matches the data type of the output parameter. For instance, if the output is an integer, use
DECLARE @MyVariable INT;. You cannot pass a literal value (like the number 5) into an output parameter; it must be a variable that can hold the returned value. - Modify the EXECUTE Statement: Rewrite the call to include the
OUTPUTkeyword. The corrected syntax should be:EXEC usp_GetUserCount @DepartmentID = 10, @Count = @MyVariable OUTPUT;. This tells SQL Server that the variable@MyVariableis intended to receive the value generated inside the procedure. - Execute and Verify: Run the corrected script. After the
EXECline, add aSELECT @MyVariable;statement to confirm that the procedure successfully returned the expected data into your variable. - Update Application Code: If this error is appearing in an application (such as C# or Java), check the command object parameters. Ensure the
ParameterDirectionproperty for that specific parameter is explicitly set toParameterDirection.Outputrather thanInput.
Common Mistakes
One of the most frequent mistakes is assuming that because a parameter is defined as OUTPUT in the stored procedure, it is automatically treated as such during execution. SQL Server requires the keyword in both the definition and the call. If you omit it in the call, SQL Server treats the variable as a standard input parameter, creating a mismatch that triggers Error 591.
Another common error is attempting to pass a constant value to an output parameter. For example, EXEC usp_MyProc @OutParam = 0 OUTPUT will fail because a constant cannot be modified by the procedure. You must always use a declared variable to capture output values.
Developers also frequently forget to match the data types exactly. If the stored procedure defines an output parameter as VARCHAR(50) but the calling script declares the variable as VARCHAR(10), you may not get Error 591, but you will encounter data truncation errors or unexpected crashes once the procedure attempts to assign the value.
Finally, mixing named parameters and positional parameters can lead to confusion. If you are calling a procedure by position (e.g., EXEC usp_Proc 1, @Var OUTPUT), ensure the position of the OUTPUT keyword matches the exact order of parameters defined in the procedure header.
Tips
To avoid this error in the future, always use named parameters when calling stored procedures. Instead of EXEC usp_Proc @Val1, @Val2 OUTPUT, use EXEC usp_Proc @Param1 = @Val1, @Param2 = @Val2 OUTPUT. This makes the code much more readable and prevents errors if the order of parameters in the stored procedure is changed later.
When working with complex procedures that have many output parameters, create a dedicated wrapper stored procedure or a script template. This ensures that every time the procedure is called, the OUTPUT keywords are consistently applied and the variables are correctly typed.
Use the sys.parameters system view to programmatically check which parameters in your database are marked as output. This is particularly useful if you are migrating a large number of legacy procedures and need to audit how they are being called across your application.
If you are using a stored procedure to return a single value, consider whether a scalar-valued function might be more appropriate. Functions are often easier to call within a SELECT statement and do not require the complex variable declaration and OUTPUT syntax required by stored procedures.
Troubleshooting
If you have added the OUTPUT keyword and are still experiencing issues, first check for data type mismatches. Ensure the variable declared in your batch is the exact same type and length as the one in the procedure. A NVARCHAR in the procedure and a VARCHAR in the call can sometimes cause implicit conversion issues that mask the root cause.
If the error persists in an application environment, clear your project's build cache and restart the application. Occasionally, cached versions of the stored procedure metadata in the application's data access layer can cause the driver to send an incorrect request to the server.
Check for triggers or nested stored procedures. If your main procedure calls another procedure, the OUTPUT keyword must be passed through every layer. If Procedure A calls Procedure B, and Procedure B has an output parameter, Procedure A must also define that parameter as OUTPUT when it calls Procedure B, and the final caller must also use the OUTPUT keyword.
Verify that you are not accidentally using the RETURN value as an output parameter. The RETURN statement in SQL Server is strictly for integer status codes (usually 0 for success). If you need to return strings, dates, or multiple values, you must use OUTPUT parameters. Mixing these two concepts often leads to logic errors and syntax confusion.
Frequently Asked Questions
Why do I need the OUTPUT keyword in two places?
The keyword in the procedure definition tells SQL Server the parameter can return a value. The keyword in the EXECUTE statement tells SQL Server that the caller expects a value and has provided a variable to store it.
Can a parameter be both input and output?
Yes. If you define a parameter as OUTPUT, you can pass a value into it, the procedure can modify that value, and then pass the updated value back to the caller. This is effectively an INOUT parameter.
What is the difference between RETURN and OUTPUT?
RETURN is used for a single integer value, typically indicating the success or failure of the procedure. OUTPUT parameters can be of any data type and you can have multiple output parameters in a single procedure.
Does Error 591 happen with Table-Valued Parameters?
No, Table-Valued Parameters (TVPs) are read-only. You cannot mark a TVP as OUTPUT. To return a table, you should use a SELECT statement at the end of the procedure or use a temporary table.
Can I use a literal value instead of a variable for an output parameter?
No. Because an output parameter must be able to store a value returned by the server, it requires a memory location (a variable). Passing a literal like 'Test' will result in a syntax error.