SA0192 : Procedure returns more than one result set

Ensuring that stored procedures return only a single result set is crucial for optimal performance and maintainability.

Description

In SQL Server, a common issue arises when stored procedures are designed to return multiple result sets. This can lead to unexpected behavior in applications consuming these procedures and can complicate the integration with client-side applications expecting a single result set.

For example:

SQL
1-- Example of a stored procedure returning multiple result sets
2CREATE PROCEDURE RetrieveData
3AS
4BEGIN
5    SELECT * FROM Customers;
6    SELECT * FROM Orders;
7END;

This example demonstrates how executing the stored procedure RetrieveData will send multiple result sets to the client, which can confuse consuming applications that are not designed to handle more than one result.

  • Confuses client applications expecting a single result set, potentially causing errors or unintended behavior.

  • Increases the complexity of database and application maintenance due to handling of multiple sets of data.

How to fix

This section guides you through ensuring that a stored procedure returns only a single result set to avoid potential issues with client applications.

Follow these steps to address the issue:

  1. Review the stored procedure to identify any SELECT statements that may contribute to multiple result sets.

  2. Determine if it is necessary to return all result sets, or if logic can be combined or consolidated into a single result set. Use UNION or JOIN operations to merge data into one set if possible.

  3. Modify the stored procedure to ensure only one result set is returned. Alter the SELECT queries as required.

For example:

SQL
 1-- Example of corrected stored procedure with a single result set
 2CREATE PROCEDURE RetrieveSingleData
 3AS
 4BEGIN
 5    SELECT 
 6        Customers.CustomerID, 
 7        Customers.CustomerName, 
 8        Orders.OrderID, 
 9        Orders.OrderDate
10    FROM Customers
11    JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
12END;

Scope

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

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
3 hours per issue.
Categories

Design Rules, Bugs

Additional Information
Example Test SQL
SQL
 1CREATE PROCEDURE test.spTest_SA0192
 2 @Param1 INT
 3AS 
 4SET  NOCOUNT ON;
 5
 6DECLARE  @Var1 INT
 7
 8IF(@Param1 = 1) SELECT  1
 9ELSE
10BEGIN
11    WITH  c( col1, col2) AS (SELECT  1, 2)
12    SELECT  2
13SELECT  2
14END
15
16SELECT  3
17
18SELECT  @Var1 = 2

Analysis Results
  Message Line Column
1 SA0192 : Procedure returns more than one result set. 1 0
See Also

Other Resources