IF OBJECT_ID('tempdb..#rootOrg') IS NOT NULL DROP TABLE #rootOrg; IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t; DECLARE @tname VARCHAR(50); DECLARE @value VARCHAR(50); SET @tname='tbl_cfg_Project'; SET @value='994'; CREATE TABLE #t ( tablename VARCHAR(64), columnname VARCHAR(64) ) SELECT ROW_NUMBER() OVER(ORDER BY c.name) rowno, o.name AS tname, c.name AS cname INTO #rootOrg FROM syscolumns c INNER JOIN sysobjects o ON c.id = o.id WHERE o.type = 'U' AND o.name=@tname DECLARE @sql VARCHAR(MAX) DECLARE @minid INT,@count INT; SET @minid=1; SELECT @count=COUNT(*) FROM #rootOrg; WHILE @minid<=@count BEGIN DECLARE @column VARCHAR(50); SELECT @column=cname FROM #rootOrg WHERE rowno=@minid; SET @sql = 'IF EXISTS(SELECT NULL FROM [' + @tname + '] ' SET @sql = @sql + 'WHERE RTRIM(LTRIM([' + @column + '])) LIKE ''%' + @value + '%'') ' SET @sql = @sql + 'INSERT INTO #t VALUES (''' + @tname + ''', ''' SET @sql = @sql + @column + ''')' PRINT @sql; EXEC(@sql) --2.5、选择下一行 SET @minid=@minid+1; END SELECT * FROM #t; IF OBJECT_ID('tempdb..#rootOrg') IS NOT NULL DROP TABLE #rootOrg; IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t;