declare cur_table cursor for with cte(num,tableName,columnName,typeName,max_length) as ( select row_number() over(order by ia.name asc) 'Num' , ia.name 'tableName',ib.name 'columnName', ic.name 'typeName',ib.max_length from test.sys.objects ia inner join test.sys.columns ib on ia.object_id = ib.object_id inner join test.sys.types ic on ib.user_type_id = ic.user_type_id where ia.type ='U' and ia.name = 'abc' ) select num,tableName,columnName,typeName,max_length from cte;
open cur_table fetch next from cur_table into @num,@tableName, @columnName,@typeName,@max_length while(@@FETCH_STATUS = 0) begin if(@typeName = 'varchar') begin set @str = ' alter table '+@tableName+' alter column '+@columnName+' nvarchar('+str(@max_length)+') ' exec(@str); end fetch next from cur_table into @num,@tableName, @columnName,@typeName,@max_length end close cur_table; deallocate cur_table;