设置与选择查询小数位

时间:2018-10-03 12:18:16

标签: sql sql-server select set

DECLARE @TotalRunBalance decimal

查询

SELECT SUM (RunBalance) AS WhatShows from Activity_Table where Engine#=1

获得的答案:2.49

查询

SET @TotalRunBalance = (SELECT SUM(RunBalance) from Activity_Table where Engine#=1)
SELECT @TotalRunBalance AS WhatShows2

显示的答案:2

任何人都知道为什么我输了0.49吗? -当我使用@TotalRunBalance变量时,它破坏了接下来的计算

1 个答案:

答案 0 :(得分:0)

您必须添加精度DECIMAL(m,a)

Example 
declare @TotalRunBalance decimal(18,3)

set @TotalRunBalance=2.49
select @TotalRunBalance

您在下面的情况

DECLARE @TotalRunBalance decimal(18,3) // add precision in decimal declaratiton


Query
SELECT SUM (RunBalance) AS WhatShows from Activity_Table where Engine#=1
Answer obtained: 2.49

Query
SET @TotalRunBalance = (SELECT SUM(RunBalance) from Activity_Table where Engine#=1)
SELECT @TotalRunBalance AS WhatShows2