标签:
Declare @db varchar(100) Declare @dbID int =4 Declare @tabName varchar(100)=‘mask‘ Declare @columnName varchar(100) = ‘‘ Declare @FixDB varchar(100)=‘ProductDB‘ --在所有的库中找一张表 while 1=1 Begin Select Top 1 @db=name,@dbID=database_id From Sys.databases where database_id>@dbID Order by database_id asc --如果指定搜索的数据库,则跳过所有非指定数据库 If (@FixDB<>@db And LEN(@FixDB)>2) Continue Begin try if(LEN(@tabName)>0) exec ( ‘If Exists( Select ‘‘‘+@db+‘‘‘,* From ‘+@db+‘.sys.tables(Nolock) where name like ‘‘%‘+@tabName+‘%‘‘ ) Select ‘‘‘+@db+‘‘‘ DBName,* From ‘+@db+‘.sys.tables(Nolock) where name like ‘‘%‘+@tabName+‘%‘‘ ‘) if(LEN(@columnName)>0) exec ( ‘If Exists( Select C.name,O.name From ‘+@db+‘.sys.columns(Nolock) C Inner Join ‘+@db+‘.sys.objects O on C.object_id=O.object_id where C.name like ‘‘%‘+@columnName+‘%‘‘) Select ‘‘‘+@db+‘‘‘ DBName,O.name TableName,C.name ColumName From ‘+@db+‘.sys.columns(Nolock) C Inner Join ‘+@db+‘.sys.objects O on C.object_id=O.object_id where C.name like ‘‘%‘+@columnName+‘%‘‘ ‘ ) End try Begin catch Print @db End catch If (@FixDB=@db And LEN(@FixDB)>2)--如果当前是指定的数据库,说明已经运行结束,跳出循环。 Break IF((Select max(database_id) From Sys.databases )=@dbID) --如果当前运行结束的数据库是最后一个,则说明运行结束,退出循环 break End
标签:
原文地址:http://www.cnblogs.com/thaughtZhao/p/4311350.html