declare @dbname sysname,@sql nvarchar(4000),@re bit,@sql1 varchar(8000) set @sql1='' declare tb cursor for select name from master..sysdatabases open tb fetch next from tb into @dbname while @@fetch_status=0 begin set @sql='set @re=case when exists(select 1 from [' [email protected]+']..sysobjects where xtype=''u'' and name=''' [email protected]+''') then 1 else 0 end' exec sp_executesql @sql,n'@re bit out',@re out if @re=1 set @[email protected]+' union all select '''[email protected]+'''' fetch next from tb into @dbname end close tb deallocate tb set @sql1=substring(@sql1,12,8000) exec(@sql1)