select ROW_NUMBER() over(orderby t.name,c.name) asRowId, t.name as tName,c.name as cName,c.is_nullable as cNull into#temp from sys.columns c innerjoin sys.tables t on t.object_id=c.object_id where c.user_type_id=106and t.type='U'and c.precision=38and c.scale=30
declare @CurRowId int--当前索引 select @CurRowId=MIN(RowId) from#temp print @CurRowId while(@CurRowId isnotnull) begin print @CurRowId declare @TableName varchar(128) declare @ColumnName varchar(128) declare @Nullable bit select @TableName=tName,@ColumnName=cName,@Nullable=cNull from#temp where RowId=@CurRowId
declare @sqlvarchar(1000)
if(@Nullable=0) begin set @sql='ALTER TABLE '+@TableName+' ALTER COLUMN '+@ColumnName+' decimal(22,5) NOT NULL' end else begin set @sql='ALTER TABLE '+@TableName+' ALTER COLUMN '+@ColumnName+' decimal(22,5) NULL' end print @sql begin try exec(@sql) end try begin catch print @sql+' 执行报错' print ERROR_MESSAGE() end catch
select @CurRowId=MIN(RowId) from#temp where RowId>@CurRowId end droptable#temp