Solved

Insert Exec cannot be nested

Posted on 2013-01-23
4
598 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
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 300 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 39

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 39

Assisted Solution

by:lcohan
lcohan earned 200 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 47
hyperlink data type in SQL 3 26
optimize stored procedure 6 24
Query to capture 5 and 9 digit zip code? 4 20
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 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