----DROP TABLE #t --SELECT ROW_NUMBER ( ) OVER( PARTITION BY tid ORDER BY tablename ) cid,* --into #t --FROM --( --SELECT TOP 100 PERCENT DENSE_RANK ( ) OVER( ORDER BY a.name ) tid, -- a.name AS tablename, -- d.name AS columnname, -- d.cname AS type --FROM sysobjects a -- JOIN (SELECT c.name AS cname,b.* FROM syscolumns b JOIN systypes c ON b.xtype=c.xusertype) d -- ON a.id = d.id --WHERE a.xtype = 'U' --ORDER BY -- a.name --) result --WHERE TYPE IN ('char','varchar','nchar','nvarchar') SET NOCOUNT ON --SELECT * FROM #t DECLARE @t TABLE (res VARCHAR(max)) DECLARE @tid INT SELECT @tid = MAX(tid) FROM #t DECLARE @tidN INT SET @tidN = 1 WHILE (@tidN < = @tid) BEGIN DECLARE @cid INT DECLARE @tname VARCHAR(60) DECLARE @cname VARCHAR(60) SELECT @cid = MAX(cid) FROM #t WHERE tid = @tid DECLARE @cidN INT SET @cidN = 1 SELECT @tname = tablename FROM #t WHERE tid = @tidn WHILE (@cidN <= @cid) BEGIN SELECT @cname = columnname FROM #t WHERE tid = @tidn AND cid = @cidn PRINT CAST(@tidn AS VARCHAR) + ' ' + CAST(@cidN AS VARCHAR) + ' ' +@tname + + ' ' + @cname INSERT @t exec ('select '+''''+@tname+'_' +@cname+''''+' from '+@tname+' where '+@cname+'=''FF''') SET @cidn = @cidn + 1 END SET @tidN = @tidN + 1 END SELECT * FROM @t t