2015-04-03 47 views
-1

我正在下面在执行语句时的错误:EXEC master.sys.sp_MSforeachdb错误

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

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'. 

如果我不使用语法像这样运行

它只循环通过AdventureWorks2014和msdb数据库。它不通过任何其他数据库循环。

sp_get_extendedproperty在master db上。

+0

注意:'sp_MSforeachdb'是臭名昭着的不可靠的。请参阅:http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/29/a-more-reliable-and-more-flexible-sp-msforeachdb.aspx – 2015-04-03 18:28:05

回答

0

使用说明位置错误。尝试这样的事情: I exec sp_MSforeachdb'use?在这里的讲话“

的休息我只是执行这一点,它工作得很好:

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

如果你的PROC是名sp_xxx并且处于主应该在所有数据库是可用的。

+0

我在每个数据库中都遇到了与以前相同的错误名称:消息102,级别15,状态1,行24 “TestDatabase”附近的语法不正确。 – user2536008 2015-04-03 18:08:02