2015-04-03 75 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