插入查询中的数字溢出
Numeric overflow in insert query
我收到以下错误:
Arithmetic overflow or division by zero has occurred. arithmetic
exception, numeric overflow, or string truncation. numeric value is
out of range.
这可以通过以下方式复制:
create table testing (avalue numeric(3,2));
和以下插入内容:
insert into testing values (328);
但是,使用以下方法效果很好:
insert into testing values (327);
328好像是错误发生的魔数。对我来说,numeric(3,2)
声明应该允许我使用 000-999 和 2 个小数位,但基于上述是错误的。
谁能解释这是为什么以及我应该如何声明我的域,就好像我想允许 0-999 和小数点后两位一样。
谢谢
声明 numeric(5, 2)
为您提供从 0.00 到 999.99 的数字。声明 numeric(3,2)
为您提供从 0.00 到 9.99 的数字。这有点像插图 here。但这些是 SQL.
中数字的标准声明
“3”是小数位数,是数字的总位数,不是小数点左边的数字。
我不确定为什么允许使用 327。
328 不是 "magic" 号码 :)
幻数是 32767 (0x7FFF)。这是 SMALLINT
类型限制。
注意:Firebird 不支持无符号整数类型。
NUMERIC
类型的限制因存储类型和规模而异。
内部存储类型根据精度为SMALLINT
、INTEGER
和BIGINT
:
精密型
1..4 - SMALLINT
5..9 - 整数
10..18 - BIGINT
所以
NUMERIC(3,2)
是 SMALLINT
内部类型最大 32767 / 100 = 327.67.
更新
Firebird 2.5 Language Reference
经过
保罗·文克努格,
德米特里·叶马诺夫和
托马斯·温克
包含比其他官方 Firebird 文档更全面的 NUMERIC
类型描述。
NUMERIC (precision, scale) is the exact number with the decimal
precision and scale specified by the and .
Syntax:
NUMERIC [precision [, scale]]
The scale of NUMERIC is the count of decimal digits in the
fractional part, to the right of the decimal point. The precision of
NUMERIC is the total count of decimal digits in the number.
The precision must be positive, the maximum supported value is 18.
The scale must be zero or positive, up to the specified precision.
If the scale is omitted, then zero value is implied, thus
meaning an integer value of the specified precision, i.e.
NUMERIC (P) is equivalent to NUMERIC (P, 0). If both the precision and
the scale are omitted, then precision of 9 and zero scale are implied,
i.e. NUMERIC is equivalent to NUMERIC (9, 0).
The internal representation of the NUMERIC data type may vary.
Numerics with the precision up to (and including) 4 are always stored
as scaled short integers (SMALLINT). Numerics with the precision up to
(and including) 9 are always stored as scaled regular integers
(INTEGER). Storage of higher precision numerics depends on the SQL
dialect. In Dialect 3, they are stored as scaled large integers
(BIGINT). In Dialect 1, however, large integers are not available,
therefore they are stored as double precision floating-point values
(DOUBLE PRECISION).
The effective precision limit for the given value depends on the
corresponding storage. For example, NUMERIC (5) will be stored as
INTEGER, thus allowing values in the precision range up to (and
including) NUMERIC (9). So beware that the declared precision is not
strictly enforced.
Values outside the range limited by the effective precision are not
allowed. Values with the scale larger than the declared one will be
rounded to the declared scale while performing an assignment.
我收到以下错误:
Arithmetic overflow or division by zero has occurred. arithmetic exception, numeric overflow, or string truncation. numeric value is out of range.
这可以通过以下方式复制:
create table testing (avalue numeric(3,2));
和以下插入内容:
insert into testing values (328);
但是,使用以下方法效果很好:
insert into testing values (327);
328好像是错误发生的魔数。对我来说,numeric(3,2)
声明应该允许我使用 000-999 和 2 个小数位,但基于上述是错误的。
谁能解释这是为什么以及我应该如何声明我的域,就好像我想允许 0-999 和小数点后两位一样。
谢谢
声明 numeric(5, 2)
为您提供从 0.00 到 999.99 的数字。声明 numeric(3,2)
为您提供从 0.00 到 9.99 的数字。这有点像插图 here。但这些是 SQL.
“3”是小数位数,是数字的总位数,不是小数点左边的数字。
我不确定为什么允许使用 327。
328 不是 "magic" 号码 :)
幻数是 32767 (0x7FFF)。这是 SMALLINT
类型限制。
注意:Firebird 不支持无符号整数类型。
NUMERIC
类型的限制因存储类型和规模而异。
内部存储类型根据精度为SMALLINT
、INTEGER
和BIGINT
:
精密型
1..4 - SMALLINT
5..9 - 整数
10..18 - BIGINT
所以
NUMERIC(3,2)
是 SMALLINT
内部类型最大 32767 / 100 = 327.67.
更新
Firebird 2.5 Language Reference 经过 保罗·文克努格, 德米特里·叶马诺夫和 托马斯·温克
包含比其他官方 Firebird 文档更全面的 NUMERIC
类型描述。
NUMERIC (precision, scale) is the exact number with the decimal precision and scale specified by the and .
Syntax: NUMERIC [precision [, scale]]
The scale of NUMERIC is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of NUMERIC is the total count of decimal digits in the number.
The precision must be positive, the maximum supported value is 18. The scale must be zero or positive, up to the specified precision.
If the scale is omitted, then zero value is implied, thus meaning an integer value of the specified precision, i.e. NUMERIC (P) is equivalent to NUMERIC (P, 0). If both the precision and the scale are omitted, then precision of 9 and zero scale are implied, i.e. NUMERIC is equivalent to NUMERIC (9, 0).
The internal representation of the NUMERIC data type may vary. Numerics with the precision up to (and including) 4 are always stored as scaled short integers (SMALLINT). Numerics with the precision up to (and including) 9 are always stored as scaled regular integers (INTEGER). Storage of higher precision numerics depends on the SQL dialect. In Dialect 3, they are stored as scaled large integers (BIGINT). In Dialect 1, however, large integers are not available, therefore they are stored as double precision floating-point values (DOUBLE PRECISION).
The effective precision limit for the given value depends on the corresponding storage. For example, NUMERIC (5) will be stored as INTEGER, thus allowing values in the precision range up to (and including) NUMERIC (9). So beware that the declared precision is not strictly enforced.
Values outside the range limited by the effective precision are not allowed. Values with the scale larger than the declared one will be rounded to the declared scale while performing an assignment.