select d.name tablename,a.name fieldname,b.name typename,a.length length,a.isnullable is_null into #t
from syscolumns a, systypes b,sysobjects d
where a.xtype=b.xusertype and a.id=d.id and d.xtype='u'
declare read_cursor cursor
for select tablename,fieldname from #t
select top 1 '_tablename ' tablename,
'fieldname ' fieldname,'typename ' typename,
'length' length,'is_null' is_null,
'maxlenused' as maxlenused,'sample value ' sample,
'comment ' comment into #tc from #t
open read_cursor
fetch next from read_cursor into @tbl,@fld
while (@@fetch_status <> -1) --- failes
begin
if (@@fetch_status <> -2) -- missing
begin
set @sql=n'set @maxlen=(select max(len(cast('[email protected]+' as nvarchar))) from '[email protected]+')'
--print @sql
exec sp_executesql @sql,n'@maxlen int output',@maxlen output
--print @maxlen
set @sql=n'set @sample=(select top 1 cast('[email protected]+' as nvarchar) from '[email protected]+' where len(cast('[email protected]+' as nvarchar))='+convert(nvarchar(5),@maxlen)+')'