循环数据库时EXEC master.sys.sp_MSforeachdb错误

时间:2015-04-03 17:27:03

标签: 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, 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位于主数据库上。

1 个答案:

答案 0 :(得分:0)

use语句位置错误。尝试这样的事情: 一世    exec sp_MSforeachdb'使用?其余的陈述在这里'

我刚刚执行了这个并且工作正常:

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

如果您的proc是名称sp_xxx并且在master中,则它应该在所有数据库中都可用。