Skip to content

SA0240 : The stored procedure does not return result code

Stored procedures without a result code in SQL Server can hinder effective error handling and feedback, especially in systems with interdependent operations.

In SQL Server, stored procedures are designed to execute a series of statements and can optionally return a result code that indicates the success or failure of the execution. This result code provides valuable feedback about the procedure’s execution, especially in complex systems where multiple operations are interdependent.

For example:

-- Example of a stored procedure without a result code
CREATE PROCEDURE ExampleProcedure
AS
BEGIN
-- Operation without any result feedback
UPDATE TableName SET ColumnName = 'Value';
END

This code fragment lacks any mechanism to convey whether the update operation completed successfully, making error handling more difficult during execution. Ensuring your stored procedures return a result code can help determine the success or failure of an operation, enabling better error handling strategies.

  • Without a result code, clients calling the procedure lack immediate feedback on its success or failure.

  • Debugging and maintaining database applications become more challenging, as diagnostic information is limited.

`

Ensure stored procedures return a result code to indicate their execution status, facilitating better error handling and feedback mechanisms.

Follow these steps to address the issue:

1.Identify stored procedures that lack a RETURN statement to provide execution status feedback.

2.Modify the procedure to include a RETURN statement, which should be strategically placed to communicate success or failure. The RETURN value typically uses 0 for success and any non-zero value for different types of failure.

3.Test the updated procedure to ensure that the RETURN value correctly reflects the execution outcome.

For example:

-- Example of a stored procedure with a result code
CREATE PROCEDURE ExampleProcedure
AS
BEGIN
BEGIN TRY
-- Simulate an operation
UPDATE TableName SET ColumnName = 'Value';
-- Return 0 indicating success
RETURN 0;
END TRY
BEGIN CATCH
-- Return a non-zero value indicating failure
RETURN 1;
END CATCH
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.

alter procedure TestProc
@param1 int
as
begin
if(@param1 is null) return;
else return @param1*@param1;
end;
  Message Line Column
1 SA0240 : The RETURN statement does not return a result code. 5 24

Analysis Rules