2016-09-12 82 views
0

我正在尋找一個查詢,它將從所有數據庫中返回結果,但只有表存在的位置。從表中存在的所有數據庫中選擇查詢

我已經運行EXEC sp_MsForEachDb @command1 = 'select * from ?.dbo.users'但這帶回了很多錯誤,因此隱藏了數據庫確實具有這些表

+0

能否請您發佈這些錯誤爲好,這將有助於... – Aditya

+0

重複的[這](HTTP感謝://計算器.COM /問題/ 20131930/SP-msforeachdb只,包括-結果從 - 數據庫與 - 結果)? – JohnHC

回答

0

我的壞..我剛剛意識到,我可以仍然得到的結果選項卡,即使它的錯誤

所有幫助

+0

請刪除這個,這應該是一個評論。 – TheGameiswar

0
`EXEC sp_MsForEachDb @command1 = 
' use ?; if exists(select * from information_schema.tables where table_`name=''test'')begin select * from test end' 

上面的語句會隱藏所有錯誤的結果,因爲它會檢查是否存在表第一..

也可以排除系統數據庫如..

EXEC sp_MsForEachDb @command1 = ' use ?; if db_id()<=4 return if exists(select * from information_schema.tables where table_name=''test'')begin select * from test end'