Skip to content

Cannot get results from Stored Procedure that uses INSERT EXEC. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JonathanHallKJR opened this issue Feb 22, 2017 · 0 comments
Open

Comments

@JonathanHallKJR
Copy link

At the bottom is a tSQLt test that replicates the "INSERT EXEC statement cannot be nested.” issue.

This has been reported a while ago the consensus is this requires a CLR procedure to work around SQL server limitations.

Replication

EXEC tSQLt.NewTestClass 'ReplicateInsertExecIssue';
GO

-- This proc returns a couple of rows
CREATE PROC ReplicateInsertExecIssue.[_DataReturnTable]
AS
	SELECT CAST('TEST' AS VARCHAR(15)) as col1, CAST(123 AS INT) AS col2 
	UNION SELECT 'TEST', 456
GO

-- This proc returns two tables after using INSERT EXEC
CREATE PROC ReplicateInsertExecIssue.[_DataUsesInsertExec]
AS 
BEGIN
	DECLARE @cachedResults TABLE(col1 VARCHAR(15) NULL , col2 int NULL);
	INSERT INTO @cachedResults EXEC ReplicateInsertExecIssue.[_DataReturnTable];
	SELECT col1, SUM(col2) AS col2 FROM @cachedResults GROUP BY col1;
	SELECT col1, MIN(col2) AS col2 FROM @cachedResults GROUP BY col1;
END;
GO

-- This tries to test the contents returned by the stored proc
CREATE PROC ReplicateInsertExecIssue.[test Replicate INSERT EXEC cannot be nested]
AS BEGIN	
	CREATE TABLE #expected (col1 VARCHAR(15) NULL , col2 int NULL);		
	INSERT INTO #expected values ('TEST', 123+456);

	CREATE TABLE #actual (col1 VARCHAR(15) NULL, col2 int NULL);
	INSERT INTO #actual EXEC tSQLt.ResultSetFilter 1, 'EXEC ReplicateInsertExecIssue._DataUsesInsertExec';

	EXEC tSQLt.AssertEqualsTable '#expected', '#actual';
END;
GO

EXEC tSQLt.Run 'ReplicateInsertExecIssue';
go
JonathanHallKJR added a commit to JonathanHallKJR/tSQLt that referenced this issue Feb 22, 2017
JonathanHallKJR added a commit to JonathanHallKJR/tSQLt that referenced this issue Feb 22, 2017
pguyot added a commit to pguyot/tSQLt that referenced this issue Mar 30, 2023
The ResultSetInsert procedure works like ResultSetFilter but insert
results into a table which name is passed as third paramter,
`@Table`.

This works around the nested insert exec issue (tSQLt-org#18).
This is an alternative and original implementation of PR tSQLt-org#19.

Signed-off-by: Paul Guyot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants