这两个查询的结果应该是相同的。相同的数据。相同的公式。相同的演员。一个结果是在查询中针对一个表变量计算的,而另一个结果是针对变量计算的。我用临时表和永久表替换了表变量,并得到了相同的结果。SQL公式返回不一致精度
为什么我的结果不同?
DECLARE
@comm DECIMAL(20 , 6)
, @quantity INT
, @multiplier INT
, @price DECIMAL(38 , 10)
SET @comm = 210519.749988;
SET @quantity = 360000;
SET @multiplier = 1;
SET @price = 167.0791666666;
DECLARE @t AS TABLE
(
[comm] [decimal](38 , 6)
, [multiplier] [int]
, [Quantity] [int]
, [Price] [decimal](38 , 10)
)
INSERT INTO @t
VALUES
(@comm , @quantity , @multiplier , @price)
SELECT
@comm = comm
, @quantity = quantity
, @multiplier = multiplier
, @price = price
FROM
@t
SELECT
CAST(comm/quantity/multiplier/price AS DECIMAL(32 , 10))
FROM
@t
UNION ALL
SELECT
CAST(@comm/@quantity/@multiplier/@price AS DECIMAL(32 , 10));
结果
1. 0.0034990000
2. 0.0035000000
同样的结果对不同的服务器。 SQL Server 2008 R2 Web Edition,Standard和Express以及SQL Server 2012 Standard。
您的输出类型,他们并不都完全相等。我看到'@comm DECIMAL(20,6)''但[COMM] [十进制](38,6)' –