Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert Exec cannot be nested

Posted on 2013-01-23
4
Medium Priority
?
627 Views
Last Modified: 2013-01-25
I had the following statement in my stored procedure

INSERT INTO #nextMtg EXECUTE proc_nextMtgList @estID,'', 0

I got the error 'Insert Exec cannot be nested...' and I realised there is another INSERT INTO..EXECUTE stmt in the same store procedure. So I changed my line of code to

INSERT INTO #upcomMtg
Select * from openrowset('SQLOLEDB','Server=<instance\servername>;UID=<uid>;PWD=<pwd>;database=<dbname>;Trusted_Connection=yes;','EXEC <dbname>.dbo.proc_nextMtgList 526,'''', 0') AS a

Now I am getting the error 'Invalid object name #rvmyMtg'
#rvmyMtg is a temp table created in the stored procedure proc_nextMtgList.

The stored procedure proc_nextMtgList is working correctly by itself. What could be causing the invalid object error?
0
Comment
Question by:Angel02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 1200 total points
ID: 38811842
TEMP tables like #rvmyMtg have a scope limited to the current connection (I believe).  But openrowset creates a new connection, so the temp table is out of scope there.
0
 

Author Comment

by:Angel02
ID: 38812021
So what wrong am I doing here?
0
 
LVL 40

Expert Comment

by:lcohan
ID: 38812062
Actually you can...there is a trick with dynamic SQL and sp_executesql and OPENROWSET - let me find an example...

check this and look for the OPENROWSET:

http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/e66c9e71-4424-4cf3-920c-6725ffc40162/

or this similar:

http://blogs.technet.com/b/wardpond/archive/2005/08/01/408502.aspx
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 800 total points
ID: 38812114
my bad...sorry - knightEknight is right and I just noticed now that you run the SQL Code on one Server but ....#rvmyMtg is created by a stored proc running on another server so that temp table IS in that server tempDB and not in tempdb where you run the code. Would be easier to use permanent working table so you can fully qualify them and eventualy linked servers instead.

"Now I am getting the error 'Invalid object name #rvmyMtg'
#rvmyMtg is a temp table created in the stored procedure proc_nextMtgList."
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question