declare @CommandLine nvarchar(max) SET @CommandLine = 'select ''{Name}'' dbname, cc.contentid, ' + ' len(text+''x'')-len(replace(text,nchar(5),'''')+''x'') indents,' + ' len(text+''x'')-len(replace(text,nchar(13),'''')+''x'') crs,' + ' len(text+''x'')-len(replace(text,nchar(10),'''')+''x'') nls,' + ' (len(text+''x'')-len(replace(text,''\par'','''')+''x''))/4 pars,' + ' (len(text+''x'')-len(replace(text,''\line'','''')+''x''))/5 lines,' + ' case when' + ' (case when charindex(''\line'',text) > charindex(nchar(13),text) ' + ' then charindex(''\line'',text) else charindex(nchar(13),text) end)' + ' -charindex(nchar(5),text) > 0 then ''FirstLine'' else ''OtherLine'' end location, ' + 'case when gg.contentid is null then ''not grid'' else ''grid'' end isgrid' + ' from {Name}.dbo.contents cc left join {Name}.dbo.grids gg on gg.contentid = cc.contentid' + ' where text like ''%'' +nchar(5) + ''%'' ' declare @CommandLines nvarchar(max) select @CommandLines = Coalesce(@CommandLines + ' UNION' + char(10), '') + Replace(@CommandLine,'{Name}',Name) from Sys.Databases where name like 'VEPROMS%' Set @CommandLines = 'Select * from ( ' +@CommandLines + ') t1' print @CommandLines EXEC SP_EXECUTESQL @CommandLines select contentid, substring(text, 1, 20) found, len(text) textlen from veproms_FNP.dbo.contents where text like '%' + nchar(5) + '%' select text, cc.contentid, len(text) length, len(text+'x')-len(replace(text,nchar(5),'')+'x') indents, case when gg.contentid is null then 'not grid' else 'grid' end isgrid , unicode(substring(text,10,1)) uni from VEPROMS_APP.dbo.contents cc left join VEPROMS_APP.dbo.grids gg on gg.contentid = cc.contentid where text like '%' +nchar(5) + '%' and len(text+'x')-len(replace(text,nchar(5),'')+'x') > 1