我有一个令人困惑的错误,我无法理解SQL Server 2008 R2。
但是当我在本地服务器(也是SQL Server 2008 R2)上尝试相同的请求时,一切正常。
所以这是提出问题的请求:
select cast(cast(1.260 as numeric(13,3)) as numeric(10,2))
我还添加了一些指示每个服务器环境的查询结果:
在本地服务器上:
---------------------------------------
1.26
(1 row(s) affected)
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64)
Apr 22 2011 19:23:43
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
ARITHABORT
---------------------------------------------------------------------------------------------
1
(1 row(s) affected)
ARITHIGNORE
---------------------------------------------------------------------------------------------
NULL
(1 row(s) affected)
ANSI_WARNINGS
---------------------------------------------------------------------------------------------
1
(1 row(s) affected)
在远程服务器上:
Msg 8115,Level 16,State 7,Line 1
算术溢出错误将数字转换为数据类型数字。
Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
Jun 17 2011 00:54:03
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.0 <X64> (Build 6002: Service Pack 2) (Hypervisor)
(1 row(s) affected)
ARITHABORT
------------------------------------------------------------------------------------------------------------
1
(1 row(s) affected)
ARITHIGNORE
------------------------------------------------------------------------------------------------------------
NULL
(1 row(s) affected)
ANSI_WARNINGS
------------------------------------------------------------------------------------------------------------
1
(1 row(s) affected)
我的问题是如何重现远程服务器上出现的问题。如您所见,两个服务器上的参数ARITH ...和ANSI_ ..是相同的。 SQL Server上的那种错误是否有任何配置?
答案 0 :(得分:11)
NUMERIC_ROUNDABORT选项为开启
当SET NUMERIC_ROUNDABORT为ON时,在表达式中发生精度损失后会生成错误。当OFF时,精度损失不会产生错误消息,结果会四舍五入到存储结果的列或变量的精度。
通常这是OFF,因为ON索引视图等可能会失败。
我永远不会改变这一点。
SET NOCOUNT ON;
GO
PRINT 'ON'
set NUMERIC_ROUNDABORT ON;
select cast(cast(1.260 as numeric(13,3)) as numeric(10,2));
GO
PRINT 'OFF'
set NUMERIC_ROUNDABORT OFF;
select cast(cast(1.260 as numeric(13,3)) as numeric(10,2));
GO
给出
ON
---------------------------------------
Msg 8115, Level 16, State 7, Line 3
Arithmetic overflow error converting numeric to data type numeric.
OFF
---------------------------------------
1.26