Solved

An INSERT EXEC statement cannot be nested

Posted on 2008-06-20
1
4,514 Views
Last Modified: 2012-05-05
When I try to capture the result set from sp_help_job using the following statement, it works fine:

INSERT dbname.dbo.tablename
EXEC [LinkedServer\Instance].msdb.dbo.sp_help_job @execution_status = 1
GO

If I try the following statement, it fails:

INSERT dbname.dbo.tablename
EXEC msdb.dbo.sp_help_job @execution_status = 1

with this error message:

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

I'm guessing that this is because sp_help_job calls sp_get_composite_job_info which has an INSERT...EXEC statement itself.  Why does this work for the linked server, in that case?  Is it something to do with the way the result set is returned?  How can the result set be captured on a local server?  I've tried setting up a linked server pointing back at itself but that doesn't work.

Thanks.  
0
Comment
Question by:mr_omid
1 Comment
 

Accepted Solution

by:
mr_omid earned 0 total points
ID: 21847859
I found that this works:
INSERT dbo.TableName
SELECT * FROM OPENROWSET('sqloledb', 'server=servername;trusted_connection=yes', 'exec msdb.dbo.sp_help_job @execution_status=1')
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now