declare @index int; declare @sql nvarchar(2000); set @index=1; while @index<=851 begin set @sql='alter table visitlog'+CONVERT(NVARCHAR(20), @index)+' alter column [OS] nvarchar(30) null;'; set @index=@index+1; begin try exec(@sql); end try BEGIN CATCH print 'error'; print @index; print ERROR_MESSAGE(); end catch end