2010-04-13 28 views
1

我想寫一個SQL Server表的insert語句插入值1到一個小數字段。該字段是十進制類型(10,10),據我所知,這意味着它最多可以有10個數字,並且這些數字中的10個可以在小數點後面。但是,當我嘗試運行INSERT語句,我得到了以下錯誤:如果我更改字段爲十進制(11,10)的數據類型爲什麼我無法將整數值輸入到小數字段中?

Arithmetic overflow error converting int to data type numeric. 

,它突然的作品。我在這裏不瞭解什麼?我究竟做錯了什麼?

回答

12

decimal(10, 10)表示所有小數位,小數點左邊沒有數字!

在這裏看到:http://msdn.microsoft.com/en-us/library/aa258832(SQL.80).aspx_

decimal[(p[, s])]

p (precision) Specifies the maximum total number of decimal digits that can be stored, both to the left and to the right of the decimal point. The precision must be a value from 1 through the maximum precision. The maximum precision is 38. The default precision is 18.

s (scale) Specifies the maximum number of decimal digits that can be stored to the right of the decimal point. Scale must be a value from 0 through p. Scale can be specified only if precision is specified. The default scale is 0; therefore, 0 <= s <= p. Maximum storage sizes vary, based on the precision.

decimal(11,10)給你1位小數和10到右邊的左邊,所以整數1,現在適合!

編輯

使用時:decimal(p,s),認爲p有多少總位數(無論左或小數點右邊)要存儲,並s有多少的那些p數字應該在小數點右邊。

DECIMAL(10,5)=  12345.12345 
DECIMAL(10,2)= 12345678.12 
DECIMAL(10,10)=   .1234567891 
DECIMAL(11,10)=  1.1234567891 
+0

我還是很困惑。它表示比例尺是「*最大*小數點右邊可存儲的小數位數」 - 並非必須爲10. – froadie 2010-04-13 13:53:19

+1

是的,但並不意味着它是小數點的滑動點。其含義是,如果您向右側提供少於10位數字,則其餘爲零。 – Joe 2010-04-13 13:58:13

+0

@Joe,我不確定你想說什麼,但是如果你運行'declare @x decimal(5,2); set @ x = 1.123456789;選擇@x; set @ x = 1; select @ x'你會得到'1.12'和'1.00'。一旦聲明小數點後,小數點左側和右側的數字數是固定的,則不會出現小數點的滑動。 – 2010-04-13 14:05:58

相關問題