Skip to content

SA0189 : Store procedure executed without getting a result

Neglecting to capture return values from stored procedures can lead to logical errors in SQL code and incorrect result handling.

When you call a stored procedure using an EXECUTE statement, you may expect a return value that should be assigned to a variable. Failing to do so means the result from the stored procedure is ignored. This can lead to unexpected behavior, as the expected value will not be usable later in your code. Properly managing return values is crucial for maintaining accurate logic in your SQL Server applications.

For example:

-- Example of problematic query
EXECUTE SomeStoredProcedure;

In the above example, the result of SomeStoredProcedure is not captured. This could lead to several issues:

  • The developer may expect a specific value to be returned and used, which will not happen, resulting in incorrect logic in subsequent queries.

  • It can lead to confusion or bugs, especially if other parts of the code assume that a value was returned, but none was actually stored.

To correctly handle the return value of a stored procedure, it is essential to properly assign it to a local variable and manage any potential errors.

Follow these steps to address the issue:

1.Define a local variable to store the return value from the stored procedure using DECLARE . For example, DECLARE @ReturnValue INT; .

2.Use the EXEC statement to call the stored procedure, properly assigning the return value to the variable. Ensure the assignment is correct by prefixing the variable with @ . For instance: EXEC @ReturnValue = SomeStoredProcedure; .

3.After the execution of the stored procedure, assess the @ReturnValue to handle different outcomes effectively. Include error handling mechanisms based on the return value.

For example:

DECLARE @ReturnValue INT;
EXEC @ReturnValue = SomeStoredProcedure;
IF @ReturnValue <> 0
BEGIN
-- Handle error scenario
PRINT 'An error occurred.';
END
ELSE
BEGIN
-- Proceed with normal flow
PRINT 'Procedure executed successfully.';
END

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

Rule has no parameters.

The rule does not need Analysis Context or SQL Connection.

13 minutes per issue.

Design Rules, Bugs

There is no additional info for this rule.

USE AdventureWorks2012;
DECLARE @result INT
EXEC @result = dbo.uspGetWhereUsedProductID 819, '20050225';
EXECUTE dbo.uspGetWhereUsedProductID 819, '20050225';
  Message Line Column
1 SA0189 : Store procedure executed without getting a result. 6 0

Analysis Rules