我有一个带有多个数据库(CustomerA,CustomerB,....)的SQL Server数据库,它们都具有相同的结构。
是否可以立即对所有数据库(CustomerA,CustomerB,....)执行查询?
下面是一台服务器:
Use CustomerA
GO
select * from Loggin.NLog where messages like '%error%'
我想要的结果是:
CustomerA
Row with error
Row with error
CustomerB
Row with error
答案 0 :(得分:2)
答案 1 :(得分:2)
您可以考虑使用(未记录的)sp sp_MSforeachdb
:
USE master;
GO
EXEC sp_MSforeachdb 'USE ?
IF (''?'' NOT IN (''master'',''msdb'',''tempdb'',''model'')) BEGIN
PRINT ''Running query on ?'';
SELECT ''?'' AS DatabaseName, * FROM Loggin.NLog WHERE messages LIKE ''%error%'';
END';