Q:

Finding Max Length via a loop

I have this following query that returns two columns table names and field names:
sample s sample record:  table ='TABLOCTYP' field name='TYPDES', The query returns records 206 rows. We are using SQL Server 2012.
 SELECT 
   CODFIC_0 AS 'Table',
   ATABZON.CODZONE_0 AS 'Field' 
   FROM LIVE.AMSKZON
          INNER JOIN LIVE.ATABZON ON AMSKZON.CODTYP_0 = ATABZON.CODTYP_0
 WHERE ATABZON.CODTYP_0 ='DES'
 AND CODMSK_0 IS NOT NULL
 GROUP BY CODFIC_0, ATABZON.CODTYP_0
 ORDER BY 1 

Image one is the results of my first query
I am looking to take these results and pass them via maybe table valued parameter maybe a loop. Something like whatis below for every row in my table.  I am not sure how to do the from peace or scan may table.  
  DECLARE @table        nvarch(15)
  DECLARE @field        nvarch(25)

  sET @table = 'ABATABT'
  SET @field ='NOMABT'
  SELECT MAX(LEN(@field),
  FROM @table 

My results will be max field length in the table and contents of field.
I am needing to know which records have a max length >12 so I can clean them up before importing into my new system. So I need the field contents displayed to know what I need to clean up.
I am getting Inccorrect Syntec near 'Group By'   
 exec ('select  
'''+@tab_name+''','''+@col_name+''',max(len('+@col_name+')),'''+@col_name+''' 
from [xxxx].'+@tab_name+'''Group By'''+@col_name+'''Having 
max(len('+@col_name+'))>12''')

 set @counter -= 1
end

A:

You can use dynamic SQL to get the details of the columns for each of the table_column pair from YourTable.
try the following
drop table if exists #temp
create table dbo.result_tab (TableName varchar(100), FieldName varchar(100), Max_Length int, Max_Value varchar(1000))
select *, ROW_NUMBER() over (order by (select null)) rn into #temp from dbo.YourTable

declare @counter int = (select count(1) from #temp)
declare @tab_name varchar(100)
declare @col_name varchar(100)

while (@counter > 0)
begin
    set @tab_name = (select TableName from #temp where rn = @counter)
    set @col_name = (select FieldName from #temp where rn = @counter)

    print('insert into result_tab select '''+@tab_name+''','''+@col_name+''',max(len('+@col_name+')), null from dbo.'+@tab_name+'')
    exec ('insert into result_tab select '''+@tab_name+''','''+@col_name+''',max(len('+@col_name+')), null from dbo.'+@tab_name+'')
    print('update result_tab set Max_Value = (select top 1 '+@col_name+' from '+@tab_name+' where len('+@col_name+') = (select Max_Length from dbo.result_tab where TableName = '''+@tab_name+''' and FieldName = '''+@col_name+''')) where TableName = '''+@tab_name+''' and FieldName = '''+@col_name+'''')
    exec ('update result_tab set Max_Value = (select top 1 '+@col_name+' from '+@tab_name+' where len('+@col_name+') = (select Max_Length from dbo.result_tab where TableName = '''+@tab_name+''' and FieldName = '''+@col_name+''')) where TableName = '''+@tab_name+''' and FieldName = '''+@col_name+'''')

    set @counter -= 1
end

select * from result_tab

please see the demo here.
HTH.

