2013-07-18 18 views
0

我有這個疑問:如果我沒有列命運,如何避免sql server中的算術溢出?

SELECT 
    client, 
    month1, 
    month2, 
    month3, 
    month4, 
    month5, 
    month6, 
    (isnull(month1, 0) + 
    isnull(month2, 0) + 
    isnull(month3, 0) + 
    isnull(month4, 0) + 
    isnull(month5, 0) + 
    isnull(month6, 0)) /6 as prom 
FROM client_total_sales 

月份可以有NULL值。

我試了一輪,轉換數據類型,但問題仍然存在。

SQL Server Management Studio在查詢加載期間顯示錯誤,我的意思是在錯誤出現之前查看一些結果。

錯誤消息: 男士。 8115,Level 16,State 2,Line 1 Error de desbordamientoaritmético(算術溢出錯誤)al convertir expression al tipo de datos int。

+0

你在問什麼?如何避免SSMS崩潰?你有沒有嘗試過多次?結果應該輸出多少行?你現在讀什麼關於SSMS崩潰?與此同時,看[這裏](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/06/25/what-to-do-when-management-studio-hangs.aspx),也許它有幫助。 – OzrenTkalcecKrznaric

+0

我的意思是在錯誤出來之前看到一些結果。 –

+0

client_total_sales是一個視圖嗎? – usr

回答

1

如果錯誤是在計算列prom可以強制推廣,可以通過鑄造計算的參數,像這樣的一個容納更多的數據的數據類型Arithmetic Overflow

SELECT 
    client, month1, month2, month3, month4, month5, month6, 
    (
    cast(isnull(month1, 0) as bigint) + 
    isnull(month2, 0) + isnull(month3, 0) + 
    isnull(month4, 0) + isnull(month5, 0) + 
    isnull(month6, 0)) /6 as prom 
FROM client_total_sales 

不知道什麼數據類型monthX是我選擇BIGINT的例子。 SQL Server不會自動升級到BIGINT,所以你必須自己做。