Sql server EXEC master.sys.sp_MSforeachdb在数据库中循环时出错

Sql server EXEC master.sys.sp_MSforeachdb在数据库中循环时出错,sql-server,Sql Server,我在执行语句时遇到以下错误: EXEC master.sys.sp_MSforeachdb 'INSERT INTO AuditDatabase.dbo.[DataDictionary] exec sp_get_extendedproperty use [?] "?"' Msg 102, Level 15, State 1, Line 23 Incorrect syntax near 'master'. Msg 102, Level 15, State 1, Li

我在执行语句时遇到以下错误:

EXEC master.sys.sp_MSforeachdb 
    'INSERT INTO AuditDatabase.dbo.[DataDictionary] 
         exec sp_get_extendedproperty use [?] "?"'

Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'master'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'tempdb'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'model'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'msdb'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'AdventureWorks2014'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'TestDatabase'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'AuditDatabase'.
如果我在不使用以下语法的情况下运行它:

EXEC master.sys.sp_MSforeachdb 
    'INSERT INTO AuditDatabase.dbo.[DataDictionary] 
        exec sp_get_extendedproperty "?"'
它只在AdventureWorks2014和msdb数据库中循环。它不会在任何其他数据库中循环


sp_get_extendedproperty位于主数据库上。

use语句位于错误的位置。试着这样做: 我 exec sp_MSforeachdb的使用?下面是声明的其余部分

我刚刚执行了这个,效果很好:

exec sp_MSforeachdb 'use ? select * from sys.objects;'

如果您的proc名为sp_xxx且在master中,那么它应该在所有数据库中都可用。

我在每个数据库名称上都遇到了与以前相同的错误:Msg 102,Level 15,State 1,Line 24“TestDatabase”附近的语法不正确。注意:
sp_MSforeachdb
是出了名的不可靠。见: