debugsqlMinor
how to avoid the "An INSERT EXEC statement cannot be nested" exception on this situation?
Viewed 0 times
cannotthistheinsertexceptionstatementsituationavoidnestedhow
Problem
when I run the following script, it runs fine:
but when I use my own sp_foreachdb procedure, the source code is on this link below:
A more reliable and more flexible sp_MSforeachdb
I get an exception (please note I have added exception handling on that procedure)
declare @temp table
(
name varchar(255),
field varchar(255),
filename varchar(255),
filegroup varchar(255),
size varchar(255),
maxsize varchar(255),
growth varchar(255),
usage varchar(255)
);
INSERT @temp
exec sp_msforeachdb @command1='use ?; Exec sp_helpfile;'but when I use my own sp_foreachdb procedure, the source code is on this link below:
A more reliable and more flexible sp_MSforeachdb
declare @temp table
(
name varchar(255),
field varchar(255),
filename varchar(255),
filegroup varchar(255),
size varchar(255),
maxsize varchar(255),
growth varchar(255),
usage varchar(255)
);
INSERT @temp
exec sp_foreachdb @command='use ?; Exec sp_helpfile;'I get an exception (please note I have added exception handling on that procedure)
--EXCEPTION WAS CAUGHT--
THE ERROR NUMBER:8164
SEVERITY: 16
STATE: 1
PROCEDURE: sp_foreachdb
LINE NUMBER: 165
ERROR MESSAGE:
An INSERT EXEC statement cannot be nested.
------------------------------------ the sql ------------------------------------
SELECT name FROM sys.databases WHERE 1=1 AND state_desc = N'ONLINE' AND is_read_only = 0
Msg 16916, Level 16, State 1, Procedure sp_foreachdb, Line 239
A cursor with the name 'c' does not exist.
Msg 16916, Level 16, State 1, Procedure sp_foreachdb, Line 240
A cursor with the name 'c' does not exist.
(0 row(s) affected)Solution
The source code for Aaron's
According to your error message:
An INSERT EXEC statement cannot be nested.
So code like that below will not be valid as it is nesting
sp_foreachdb contains the following line:INSERT #x EXEC sp_executesql @sql;According to your error message:
An INSERT EXEC statement cannot be nested.
So code like that below will not be valid as it is nesting
INSERT xxx EXEC xxx code.INSERT @temp
exec sp_msforeachdb @command1='use ?; Exec sp_helpfile;'Code Snippets
INSERT @temp
exec sp_msforeachdb @command1='use ?; Exec sp_helpfile;'Context
StackExchange Database Administrators Q#160739, answer score: 2
Revisions (0)
No revisions yet.