开发者

Why can't I enter an integer value into a decimal field?

开发者 https://www.devze.com 2022-12-27 05:42 出处:网络
I\'m trying to write an insert statement for a SQL Server table that inserts the value 1 into a decimal field. The field is of the type decimal(10, 10) which, as far as I understand, means that it can

I'm trying to write an insert statement for a SQL Server table that inserts the value 1 into a decimal field. The field is of the type decimal(10, 10) which, as far as I understand, means that it can have up to 10 digits altogether, and up to 10 of those digits can be after the decimal point. But, when I try to run the insert statement I get the following error:

Arithmetic overflow error converting int to data type numeric.

If I change the data type of the field to decimal(11, 10), it suddenly 开发者_运维技巧works. What am I not understanding here? What am I doing wrong?


decimal(10, 10) means all decimal places, no digits to the left of the decimal point!

see here: 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) gives you 1 digit the the left of the decimal and 10 to the right, so integer 1 fits now!

EDIT

when using: decimal(p,s), think of p as how many total digits (regardless of left or right of the decimal point) you want to store, and s as how many of those p digits should be to the right of the decimal point.

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

精彩评论

暂无评论...
验证码 换一张
取 消