Попробуйте:
Create PROCEDURE Pro_FindTable
(@tableName VARCHAR(MAX))
AS
BEGIN
SET NOCOUNT ON;
DECLARE @name VARCHAR(MAX),
@dbid INT;
DECLARE C CURSOR FAST_FORWARD FOR(
SELECT name,
database_id
FROM sys.databases);
OPEN C;
FETCH NEXT FROM C
INTO @name,
@dbid;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @query NVARCHAR(MAX)
= 'IF EXISTS(SELECT name FROM(SELECT name, COUNT(*)Over(Order By (Select Null)) as RN FROM(SELECT '''
+ @name + ''' AS name UNION ALL SELECT name FROM [' + @name
+ '].sys.tables WHERE type=''U'' AND name = ''' + @tableName
+ ''') as K)as K Where RN>1)
Select '''+ @name + '''';
EXEC (@query);
FETCH NEXT FROM C
INTO @name,
@dbid;
END;
CLOSE C;
DEALLOCATE C;
END;
И назовите это так:
EXEC Pro_FindTable 'MyTable'
Результатом будут все базы данных, в которых есть таблица с именем «MyTable»