简体   繁体   English

回滚错误SQL Server 2008

[英]Round through error SQL Server 2008

I found an weird thing in SQL Server 2008 today. 我今天在SQL Server 2008中发现了一件奇怪的事情。 I write a ROUND function, and I test this randomly. 我写了一个ROUND函数,我随机测试。

From 9.50-9.99, this line of code: 从9.50-9.99,这行代码:

SELECT ROUND(X.XX, 0);  

throws this error: 抛出此错误:

An error occurred while executing batch. 执行批处理时发生错误。
Error message is: Arithmetic Overflow. 错误消息是:算术溢出。

but from 9.00-9.49 it is working just fine. 但是从9.00-9.49开始工作得很好。

Can anybody please let me know what's wrong with that code? 任何人都可以让我知道该代码有什么问题吗?

This is because it is trying to implicitly fit this into a numeric(3,2) datatype, which 10.00 will over flow. 这是因为它试图将其隐式地装入numeric(3,2)数据类型中,10.00将过度流动。 You need to cast it to a larger datatype: select round(cast(9.50 as numeric(4,2)),0) 您需要将其转换为更大的数据类型: select round(cast(9.50 as numeric(4,2)),0)

You can run this code to see what the implicit datatype & precision is: 您可以运行此代码以查看隐式数据类型和精度是什么:

select SQL_VARIANT_PROPERTY (cast(9.50 as SQL_VARIANT), 'BaseType'),
         SQL_VARIANT_PROPERTY (cast(9.50 as SQL_VARIANT), 'Precision'),
         SQL_VARIANT_PROPERTY (cast(9.50 as SQL_VARIANT), 'Scale') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM