The result of the two queries should be identical. The same data. The same formula. The same cast. One result is computed in a query against a table variable, and the second for variables. I replaced the table variable with a temporary table and a constant table with the same results.
Why are my results different?
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));
Result
1. 0.0034990000 2. 0.0035000000
The same results against different servers. SQL Server 2008 R2 Web Standard, Standard and Express, and SQL Server 2012 Standard.
source share