2013-04-16 36 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'。您可能還需要爲收斂創建臨時列(請參閱更新的答案)。 –

相關問題