Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 为什么SQL Server在将int转换为数据类型numeric时抛出算术溢出错误?_Sql Server_Variables_Types_Numeric - Fatal编程技术网

Sql server 为什么SQL Server在将int转换为数据类型numeric时抛出算术溢出错误?

Sql server 为什么SQL Server在将int转换为数据类型numeric时抛出算术溢出错误?,sql-server,variables,types,numeric,Sql Server,Variables,Types,Numeric,运行此代码时,SQL Server Management Studio引发了一个错误: declare @percentage numeric(3,2) set @percentage = cast(15 as numeric(3,2)) 但当我将数字声明更改为 declare @percentage numeric(4,2) set @percentage = cast(15 as numeric(4,2)) 一切顺利 数字数据类型有限制吗?numeric定义总位数,然后是小数点后的数字

运行此代码时,SQL Server Management Studio引发了一个错误:

declare @percentage numeric(3,2)
set @percentage = cast(15 as numeric(3,2))
但当我将数字声明更改为

declare @percentage numeric(4,2)
set @percentage = cast(15 as numeric(4,2))
一切顺利


数字数据类型有限制吗?

numeric定义总位数,然后是小数点后的数字

数字(3,2)最多只能容纳9.99。

让我们看看数字(3,2)。这意味着数据有3位,其中两位在小数点的右边,只有一位在小数点的左边。15在小数点左边有两位。顺便说一句,如果你可能有100作为一个值,我会增加到数字(5,2)

数字(3,2)的意思是:总共3位,小数点后2位。所以在小数点之前只有一个小数点


请尝试使用数字(5,2)——小数点前三位,小数点后两位。

精度和刻度经常被误解。在数字(3,2)中,您希望总共有3位数字,但小数点右侧有2位。如果希望15=>15.00,则前导1会导致溢出(因为如果希望小数点右侧有两个数字,则左侧只能容纳一个以上的数字)。对于4,2,没有问题,因为所有4个数字都适合