2013-04-16 33 views
1

因为那是数据提供者的格式,所以我在SQL Server 2012数据库中有一个字段,它当前是十进制的(30,20)。然而,大数小数是完全不必要的,并且想将它缩减为像十进制(16,7)这样的表,因为该表具有数百万行。减少十进制数据类型precision&scale并避免算术溢出错误

我尝试以下SQL:

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

但是,这导致错误:

Arithmetic overflow error converting numeric to data type numeric. 

什么是改变这一领域的最好的方法是什么?我明显地意识到错误正在发生,因为列中的值超过了新的精度。我想知道的是,如果有一个脚本可以切断小数点后第7位的任何数字,然后转换列数据类型?

回答

0

减少到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; 
+0

尝试这种解决方案...它的格式作出的所有值:### ####### 00000000000000000000。当我重新运行alter column脚本时,仍然会得到相同的错误。 – Ricketts

+0

请尝试'convert'。您可能还需要为收敛创建临时列(请参阅更新的答案)。 –

相关问题