1

So I have declared a query within a varchar variable like:

DECLARE @sql VARCHAR(MAX) = 'select * from table'

I've also declared a temp table @Results that matches the column structure of the query.

I then call:

INSERT INTO @Results
EXEC (@sql)

The error I get is:

An INSERT EXEC statement cannot be nested.

I would also like to add that the query executes fine without the part

INSERT INTO @Results

What could be causing it? Any tips will be greatly appreciated

6
  • i don't see any issue with the query. Probably something in your dynamic query. Can you show your dynamic query ? Commented Oct 18, 2019 at 8:39
  • It's better to use column names instead of * in your select statement so you can see whether the data is inserted into your @Results in the correct column order Commented Oct 18, 2019 at 8:43
  • You can't chain together more than once an INSERT INTO + EXEC. Your Dynamic SQL probably has one already, so this is why the engine is telling you that you can't nest them. I'd recommend reading this paper on all different ways to share data between procedures sommarskog.se/share_data.html . Personally I use point 4.1 (sharing temp table) in most of our projects. Commented Oct 18, 2019 at 8:44
  • Well, I can't really show the real query to the public atm. Just to clarify, I'm not using the * operator in the query and the query consists of several insert/exec/select statements. I just have no idea what could be causing it since it doesn't really point me to the problem and the query executes if I remove the 'INSERT INTO @Results' statement. Commented Oct 18, 2019 at 8:46
  • That error usually happens if you try to insert table exec someStoredProcedure and the stored procedure is trying to insert into a temp table internally. Commented Oct 18, 2019 at 8:46

1 Answer 1

2

You can't stack together more than 1 INSERT INTO + EXEC, this is a limitation from SQL Server.

DECLARE @Test TABLE (Number INT)

INSERT INTO @Test (Number)
EXEC ('
    CREATE TABLE #InnerTable (SomeColumn INT);

    INSERT INTO #InnerTable (SomeColumn)
    EXEC (''SELECT 1'');

    SELECT 1;')

Msg 8164, Level 16, State 1, Line 4 An INSERT EXEC statement cannot be nested.

If we remove the inner INSERT INTO + EXEC...

DECLARE @Test TABLE (Number INT)

INSERT INTO @Test (Number)
EXEC ('
    CREATE TABLE #InnerTable (SomeColumn INT);

    SELECT 1;')

Succeed!


There are multiple ways to workaround this limitation, however most of them will require modifying the EXEC content. You can find an exhaustive explanation here. The one I usually use with my procedures is sharing a temporary table. You need to create a temporary table outside the EXEC then load it inside. The table is still accessible outside the EXEC scope because it was created outside.

IF OBJECT_ID('tempdb..#Test') IS NOT NULL
    DROP TABLE #Test

CREATE TABLE #Test (Number INT)

EXEC ('INSERT INTO #Test (Number) SELECT 1')

SELECT * FROM #Test

Downside of this approach is that the EXEC part might fail if the temporary table wasn't created or it was created with wrong column names or data types. Keep in mind that this won't work with variable tables, they need to be temporary (at least) to be accessible inside the EXEC scope.

Sign up to request clarification or add additional context in comments.

2 Comments

I've managed to solve my problem using your help. Thanks for the detailed explanation!
+1. I found this to be very useful. I also like the link that you posted for 'How to Share Data between Stored Procedures'. Thank you for answering and sharing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.