Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error: an insert exec statement cannot be nested – quick tips
Next
Answered
Stella O'Ryan
  • 33
Stella O'Ryan
Asked: May 18, 20222022-05-18T16:22:36+00:00 2022-05-18T16:22:36+00:00In: sql

Error: an insert exec statement cannot be nested – quick tips

  • 33

. Advertisement .

..3..

. Advertisement .

..4..

The error

INSERT EXEC statement cannot be nested
Cannot use the ROLLBACK statement within an INSERT-EXEC statement.

occurred when I execute the Sp1, it will display the error (I have 3 stored procedures Sp1, Sp2 and Sp3. The first (Sp1) will run the second (Sp2) which will save the returned data to @tempTB1 while the second will execute the third (Sp3) as well as save the data in @tempTB2.). I’ve tried to fix it for some days but have not resolved it yet. Can someone suggest a relevant solution to fix the an insert exec statement cannot be nested issue? Much appreciate your support.

insert exec
  • 2 2 Answers
  • 487 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-10T09:34:56+00:00Added an answer on June 10, 2022 at 9:34 am

    The cause:

    It looks like you are trying to ‘bubble up’ data from a series of procedures which are stored, but there is a restriction on SQL Server that you can only have one active INSERT-EXEC at a given time. Therefore, this error happens.

    Solution:

    To find the way solve this error, you can read this article: How To Share Data Between Stored Procedures. It is a detailed article about patterns that can be used to solve this problem.

    Another solution I recommend you is converting Sp3 into a Table value function.

    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Chloë Barre
    2022-05-25T20:29:49+00:00Added an answer on May 25, 2022 at 8:29 pm

    This is the only way to do it in SQL Server without having to create a huge convoluted function or execute sql string calls, which would be terrible.

    1. Create a temp table
    2. openrowset your stored procedure data into it

    EXAMPLE:

    INSERT INTO #YOUR_TEMP_TABLE
    SELECT * FROM OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','set fmtonly off EXEC [ServerName].dbo.[StoredProcedureName] 1,2,3')

    Note – You must use’setfmtonlyoff’ and you can’t add dynamic sql inside the openrowset calls, either for the string that contains your stored procedure parameters, or for the table’s name. You must use temp tables instead of table variables. It is more efficient in most cases.

    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.