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 ‘‘‘[email protected]+‘‘‘,* From ‘[email protected]+‘.sys.tables(Nolock) where name like ‘‘%‘[email protected]+‘%‘‘ ) Select ‘‘‘[email protected]+‘‘‘ DBName,* From ‘[email protected]+‘.sys.tables(Nolock) where name like ‘‘%‘[email protected]+‘%‘‘ ‘) if(LEN(@columnName)>0) exec ( ‘If Exists( Select C.name,O.name From ‘[email protected]+‘.sys.columns(Nolock) C Inner Join ‘[email protected]+‘.sys.objects O on C.object_id=O.object_id where C.name like ‘‘%‘[email protected]+‘%‘‘) Select ‘‘‘[email protected]+‘‘‘ DBName,O.name TableName,C.name ColumName From ‘[email protected]+‘.sys.columns(Nolock) C Inner Join ‘[email protected]+‘.sys.objects O on C.object_id=O.object_id where C.name like ‘‘%‘[email protected]+‘%‘‘ ‘ ) End try Begin catch Print @db End catch If (@[email protected] And LEN(@FixDB)>2)--如果当前是指定的数据库,说明已经运行结束,跳出循环。 Break IF((Select max(database_id) From Sys.databases )[email protected]) --如果当前运行结束的数据库是最后一个,则说明运行结束,退出循环 break End
时间: 2024-11-14 12:20:07