如果您知道确切的 DB 数量:
DECLARE @sql NVARCHAR(MAX) = N'';
SELECT @sql += N'
UNION ALL
SELECT C1,C2,C3
FROM db' + CONVERT(VARCHAR(2), n) + '.dbo.A'
FROM
(
SELECT TOP (50) n = ROW_NUMBER()
OVER (ORDER BY [object_id])
FROM sys.all_columns
) AS x;
SET @sql = STUFF(@sql, 1, 11, '') + ';';
PRINT @sql;
--EXEC sp_executesql @sql;
如果您不知道正好有 50 个,那么这可能会更好(它还允许您排除那些不在线的):
DECLARE @sql NVARCHAR(MAX) = N'';
SELECT @sql += N'
UNION ALL
SELECT C1,C2,C3
FROM ' + QUOTENAME(name) + '.dbo.A'
FROM sys.databases
WHERE state = 0
AND name LIKE N'db[0-9]%';
SET @sql = STUFF(@sql, 1, 11, '') + ';';
PRINT @sql;
--EXEC sp_executesql @sql;