你的位置:首页 > 软件开发 > 数据库 > 查找当前数据库服务器中某张表存在于哪个数据库中

查找当前数据库服务器中某张表存在于哪个数据库中

发布时间:2015-09-16 10:00:04
----查找当前数据库服务器中某张表存在于哪个数据库中,sqlserver2008测试通过--declare @tableName varchar(50)--这里设置要查询的表名字set @tableName=Products--清理临时表if object_id(temp ...
----查找当前数据库服务器中某张表存在于哪个数据库中,sqlserver2008测试通过--declare @tableName varchar(50)--这里设置要查询的表名字set @tableName='Products'--清理临时表if object_id('tempdb..#tmpdbs') is not null Begin  drop table #tmpdbsEndif object_id('tempdb..##tmpResults') is not null Begin  drop table ##tmpResultsEnd--手动创建全局临时表,下面插入时只能使用insert into ,不能使用select into ,后者会自动创建临时表create table ##tmpResults(  DbName varchar(50),  Name varchar(50),  XType varchar(50))Select Name,ROW_NUMBER() over(order by Name) as rowid into #tmpdbs FROM Master..SysDatabases Namedeclare @dbName varchar(50)declare @rowid intdeclare @count intset @rowid=1select @count=count(*) from #tmpdbswhile @rowid <= @countbegin  --print(@rowid)  select @dbName=[Name] from #tmpdbs where rowid=@rowid  exec ('insert into ##tmpResults Select '''+@dbName+''' as DbName,Name,xtype FROM '+@dbName+'..SysObjects Where (XType=''U'' or XType=''SN'') and Name='''+@tableName+''' ORDER BY Name')  set @rowid=@rowid+1end--查看结果select * from ##tmpResults--清理临时表if object_id('tempdb..#tmpdbs') is not null Begin  drop table #tmpdbsEndif object_id('tempdb..##tmpResults') is not null Begin  drop table ##tmpResultsEnd

原标题:查找当前数据库服务器中某张表存在于哪个数据库中

关键词:数据库

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。