- Query the specified table exists in those databases
Declare @tbname sysnameset @ TBNAME = 'Customer Information'
declare @dbname sysname, @ sql nvarchar (4000), @ re bit, @ sql1 varchar (8000) set @ sql1 = '' declare tb cursor for select name from master..sysdatabasesopen tb fetch next from tb into @dbnamewhile @@ fetch_status = 0BEGIN SET @ SQL = 'set @ re = case when @ DBNAME '] .. sysobjects where xtype = 'u' '' and name = '' ' @ tbname ' ') THEN 1 else 0 end 'exec sp_executesql @ SQL, N' @ re bit out ', @ RE OUT IF @ RE = 1 set @ SQL1 = @ SQL1 ' Union All Select '' ' @ DBNAME ' 'Fetch Next from TB INTO @dbnamendclose TBDEAllocate TBSET @ SQL1 = Substring (@ SQL1, 12, 8000) EXEC (@ SQL1)