DECLARE @prefix VARCHAR(64) = 'Dev_LDS_Dev_LSS'
DECLARE @dbname VARCHAR(256)
DECLARE @statement VARCHAR(MAX)
DECLARE @sql VARCHAR(MAX)
DECLARE db_cursor CURSOR FOR
SELECT [name]
FROM sys.databases WHERE [name] like @prefix + '%'
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @statement = 'SELECT ''' + @dbname+''' AS db, convert(VARCHAR(MAX),[name]) COLLATE SQL_Latin1_General_CP1_CI_AS AS tb FROM ' + @dbname +'.sys.tables where name like ''%CAWorkshopPriceList%'''
--SET @statement = 'IF EXISTS('+ @statement + ') ' + @statement
SET @sql = @sql + ' UNION ALL' + CHAR(13)
SET @sql = ISNULL(@sql,'') + @statement
--PRINT @sql
FETCH NEXT FROM db_cursor INTO @dbname
END
CLOSE db_cursor
DEALLOCATE db_cursor
PRINT @sql
EXECUTE(@sql)