Insert Exec cannot be nested

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

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?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

knightEknightConnect With a Mentor Commented:
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.
Angel02Author Commented:
So what wrong am I doing here?
lcohanDatabase AnalystCommented:
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:

or this similar:
lcohanConnect With a Mentor Database AnalystCommented:
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."
All Courses

From novice to tech pro — start learning today.