为什么 SQL 服务器使用 ROUND 抛出算术溢出错误?

Why SQL Server throws Arithmetic overflow error using ROUND?

为什么像

这么简单
SELECT ROUND(0.99535, 2)

returns SQL SERVER 2008 R2 中的算术溢出错误?

最终数据类型为 DECIMAL(5,5),因此 1.0 值没有位置。

SELECT ROUND(0.99535, 2) 
-- it would round to 1.0000 but it is to big for DECIMAL(5,5)
<=>
SELECT CAST(1 AS numeric(5,5))
-- Arithmetic overflow error converting int to data type numeric.

正在检查元数据:

SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set  
(N'SELECT ROUND(0.99535, 2) AS result', null, 0) ;  
-- name     system_type_name
-- RESULT   numeric(5,5)

DBFiddle Demo


为避免此问题,您可以将精度更改为 6。

SELECT ROUND(CONVERT(NUMERIC(6,5), 0.99535),2)

DBFiddle Demo 2