查詢指定的表在那些資料庫中存在(轉)

post0發表於2007-08-11
查詢指定的表在那些資料庫中存在(轉)[@more@]

declare @tbname sysname

set @tbname='客戶資料'

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 ['

+@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 @dbname

end

close tb

deallocate tb

set @sql1=substring(@sql1,12,8000)

exec(@sql1

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-944379/,如需轉載,請註明出處,否則將追究法律責任。

相關文章