查询指定的表在那些中存在

来源:互联网 发布:jdbc:sqlserver: 编辑:程序博客网 时间:2024/06/05 18:17
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

--查询指定的表在那些数据库中存在

declare@tbnamesysname
set@tbname='客户资料'

declare@dbnamesysname,@sqlnvarchar(4000),@rebit,@sql1varchar(8000)
set@sql1=''
declaretbcursorforselectnamefrommaster..sysdatabases
opentb
fetchnextfromtbinto@dbname
while@@fetch_status=0
begin
 set@sql='set@re=casewhenexists(select1from['
  +@dbname+']..sysobjectswherextype=''U''andname='''
  +@tbname+''')then1else0end'
 execsp_executesql@sql,N'@rebitout',@reout
 if@re=1set@sql1=@sql1+'unionallselect'''+@dbname+''''
 fetchnextfromtbinto@dbname
end
closetb
deallocatetb
set@sql1=substring(@sql1,12,8000)
exec(@sql1)
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击