4

2 つのクエリの結果は同一である必要があります。同じデータ。同じ式。同じキャスト。1 つの結果はテーブル変数に対するクエリで計算され、2 番目の結果は変数に対して計算されます。テーブル変数を一時テーブルと永続テーブルに置き換えて、同じ結果を得ました。

結果が異なるのはなぜですか?

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。

4

3 に答える 3