为什么这么简单
SELECT ROUND(0.99535, 2)
在SQL SERVER 2008 R2中返回算术溢出错误?
答案 0 :(得分:4)
最终数据类型为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 强>