SA0243 : Avoid INSERT-EXECUTE in stored procedures |
![]() |
The INSERT-EXECUTE statement in SQL Server, while enabling stored procedure results to be inserted into a table, can introduce stability and functionality challenges.

The INSERT-EXECUTE statement in SQL Server allows developers to insert the result set of a stored procedure directly into a table. However, it introduces several challenges that can affect the stability and functionality of the database.
For example:
1-- Problematic use of INSERT-EXECUTE 2INSERT INTO TargetTable 3EXEC StoredProcedureName;
This can lead to errors and maintenance issues due to:
-
Nesting limitation: If INSERT-EXECUTE is used within a stored procedure, neither this procedure nor any that it calls may be used in another INSERT-EXECUTE, limiting its flexibility.
-
Result set matching: The columns of the procedure’s result must exactly match the target table or specified list. Changes in the procedure output will cause the INSERT-EXECUTE to fail.
-
Transaction context: The stored procedure runs in the context of the INSERT statement’s transaction, complicating transaction management and error handling.
-
Error handling challenges: Using ROLLBACK TRANSACTION inside the called procedure is not allowed, complicating error recovery.
-
Dynamic SQL nesting issues: When using dynamic SQL with sp_executesql, the same nesting restrictions apply.

To address the potential issues with the `INSERT-EXECUTE` statement, it is recommended to refactor the query or logic to avoid using this construct. Here are detailed steps to mitigate the associated problems:
Follow these steps to address the issue:
-
Refactor the stored procedure to return results as an output parameter or a table-valued function, which can then be queried using SELECT INTO or INSERT INTO.
-
Ensure that the result set schema of the stored procedure matches the schema of the target table to prevent schema-related errors.
-
Manage transactions explicitly in the calling code rather than relying on the implicit transaction context of INSERT-EXECUTE.
-
If error handling is necessary, consider using TRY…CATCH blocks to gracefully handle errors and manage transactions in the calling procedure or batch.
-
Avoid using dynamic SQL inside stored procedures called by INSERT-EXECUTE. Refactor to remove the dependency or handle it separately.
For example:
1-- Corrected approach using a table variable 2DECLARE @TempTable TABLE (Column1 INT, Column2 VARCHAR(100)); 3-- Modify the stored procedure to return a result set suitable for SELECT INTO 4INSERT INTO @TempTable 5SELECT Column1, Column2 FROM TableValueFunction(); 6-- Proceed with any operations on @TempTable

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.


Design Rules, Bugs


SQL
1CREATE PROCEDURE TestProc 2@param1 varchar(40), 3@param2 smallint AS 4DECLARE @data AS TABLE 5 (col1 varchar(100) NOT NULL PRIMARY KEY, 6 col2 smallint NOT NULL) 7 8INSERT @data EXEC GetTestData @param1 9 10SELECT * FROM @data |

Message | Line | Column | |
---|---|---|---|
1 | SA0243 : Avoid INSERT-EXECUTE in stored procedures. | 8 | 13 |
