--如果表比较大,时间会比较长 DECLARE @searchValue NVARCHAR(50) SET @searchValue='1234' DECLARE @t TABLE ( rowNum INT IDENTITY(1,1) PRIMARY KEY, tableName NVARCHAR(50), colunName NVARCHAR(50), SQL NVARCHAR(MAX), cnt INT ) INSERT INTO @t (tableName,colunName,SQL) SELECT t.name,c.name,'select @cnt=count(1) from '+t.name +' with(nolock) where '+c.name+'='''+@searchValue+'''' FROM sys.tables AS t INNER JOIN sys.[columns] AS c ON t.[object_id]=c.[object_id] DECLARE @i INT,@iMax INT,@sql NVARCHAR(MAX),@cnt INT SELECT @i=1,@iMax= MAX(rowNum) FROM @t WHILE @i<=@iMax BEGIN SELECT @sql=sql FROM @t WHERE rowNum=@i PRINT @sql EXEC sp_executesql @sql,N'@cnt int out',@cnt OUT UPDATE @t SET SQL=@sql,cnt=@cnt WHERE rowNum=@i SET @i=@i+1 END SELECT * FROM @t WHERE cnt>0