Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

An INSERT EXEC statement cannot be nested

Posted on 2008-06-20
1
Medium Priority
?
4,755 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
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.

916 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