Prøv denne -
INDSTIL ANTAL TIL; HVIS OBJECT_ID (N'tempdb.dbo.#temp') IKKE ER NULL DROP TABLE #temp OPRET TABEL #temp ( [COUNT] INT , DB VARCHAR(50) ) DECLARE @TableName NVARCHAR(50) SELECT @TableName ='[dbo ].[KUNDER]' ERKLÆR @SQL NVARCHAR(MAX) SELECT @SQL =TING(( SELECT CHAR(13) + 'SELECT ' + QUOTENAME(navn, '''') + ', COUNT(1) FROM ' + QUOTENAME (navn) + '.' + QUOTENAME(@TableName) FRA sys.databases WHERE OBJECT_ID(QUOTENAME(name) + '.' + QUOTENAME(@TableName)) ER IKKE NULL FOR XML PATH(''), TYPE).value ('text()[1]', 'NVARCHAR(MAX)'), 1, 1, '') INSERT INTO #temp (DB, [COUNT]) EXEC sys.sp_executesql @SQL SELECT * FRA #temp t
Output (for eksempel i AdventureWorks
) -
ANTAL DB------------ ------------------------------------ ------------------19972 AdventureWorks2008R219975 AdventureWorks201219472 AdventureWorks2008R2_Live