0
我想每個數據庫在我的數據庫搜索搜索(它們都具有相同的表名)有以下幾點:查詢每一個數據庫
SELECT DISTINCT NAME FROM TABLE WHERE NAME = 'blah'
我已經試過這樣:
SET NOCOUNT ON;
IF OBJECT_ID (N'tempdb.dbo.#temp') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp
(
[COUNT] INT
, DB VARCHAR(50)
)
DECLARE @TableName NVARCHAR(50)
SELECT @TableName = '[dbo].[TABLE]'
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = STUFF((
SELECT CHAR(13) + 'SELECT ''' + name + ''', COUNT(1) FROM [' + name + '].' + @TableName
FROM sys.databases
WHERE OBJECT_ID(name + '.' + @TableName) IS NOT NULL
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
INSERT INTO #temp (DB, [COUNT])
EXEC sys.sp_executesql @SQL
SELECT *
FROM #temp t
發佈您的錯誤 – 2014-10-02 08:46:52