平台:SQL Server 2008
语言:TSQL
我有很多查询,目前采用一般形式(为简单起见)
-- Sample begin results
SELECT * from DB01.dbo.table UNION ALL
SELECT * from DB02.dbo.table UNION ALL --many other databases follow with same syntax
如何修改这些查询,以便在新数据库出现时(命名为DB39C),我确保我的查询已包含这些新记录?
--Sample end results
SELECT * from DB01.dbo.table UNION ALL
SELECT * from DB02.dbo.table UNION ALL
SELECT * from DB39C.dbo.table -- this was created as soon as a new database came into existence
我希望以编程方式确保这种情况在没有我意识的情况下发生,因为新的数据库会定期添加,我需要依赖的查询来跟上步伐。
答案 0 :(得分:0)
您可能希望了解使用类似
的内容SELECT name AS DATABASENAME
FROM master.dbo.sysdatabases
并创建动态查询