Sql 降低十进制数据类型精度&;缩放并避免算术溢出错误

Sql 降低十进制数据类型精度&;缩放并避免算术溢出错误,sql,sql-server,sql-server-2012,alter-table,alter-column,Sql,Sql Server,Sql Server 2012,Alter Table,Alter Column,我在SQL Server 2012数据库中有一个字段,当前为十进制(30,20),因为这是数据提供程序的格式。然而,这个大的小数是完全没有必要的,考虑到这个表有数百万行,我们想把它缩减到类似于十进制(16,7)的值 我尝试了以下SQL: alter table mytable alter column mycolumn decimal(16,7) 但这导致了错误: Arithmetic overflow error converting numeric to data type numeric

我在SQL Server 2012数据库中有一个字段,当前为十进制(30,20),因为这是数据提供程序的格式。然而,这个大的小数是完全没有必要的,考虑到这个表有数百万行,我们想把它缩减到类似于十进制(16,7)的值

我尝试了以下SQL:

alter table mytable alter column mycolumn decimal(16,7)
但这导致了错误:

Arithmetic overflow error converting numeric to data type numeric.
改变这个领域的最佳方式是什么?我显然意识到发生错误是因为列中的值超过了新的精度。我想知道的是,是否有一个脚本可以截断超过第七个小数点的任何数字,然后转换列数据类型?

减少到7位小数:

update mytable set newcolumn = convert(decimal(16, 7), mycolumn);

update mytable set mycolumn = null;

commit;

alter table mytable alter column mycolumn decimal(16,7)

update mytable set mycolumn = newcolumn;

commit;

alter table mytable drop column newcolumn;

尝试此解决方案…它使所有值的格式为:##########00000000000000。当我重新运行alter column脚本时,仍然会出现相同的错误。请尝试
convert
。您可能还需要为凸面创建一个临时列(请参见更新的答案)。