我正在运行一个返回小数的查询,然后将这些小数插入表变量中。当我查询表变量时,我得到整数。任何人都可以看到为什么会发生这种情况以及如何纠正?使用Sql Server 2000。
DECLARE @Nov Table(custCode varchar(10), PromiseAvg decimal, ShipAvg decimal )
INSERT INTO @Nov
SELECT JM.CustomerCode
, isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(PromiseDate, 0)) As Decimal)),0) As PromiseAvg
, isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(BOL.ShipDate, 0)) As Decimal)),0) As ShipAvg
from jobitems JI
LEFT JOIN jobmaster JM ON JI.Job_Number = JM.JobNumber
LEFT JOIN dbo.NCL_BOLDetails BD ON JM.JobNumber = BD.JobNumber AND JI.Item_Code = BD.ItemNumber
INNER JOIN dbo.NCL_BOLs BOL ON BD.BOLID = BOL.BOLID
WHERE StartDate BETWEEN '20091101' AND '20091130'
Group By JM.CustomerCode, JM.CustLongName
Order By JM.CustomerCode
Select * from @Nov
答案 0 :(得分:6)
您需要在表变量中定义小数的比例和精度。
e.g。
DECLARE @Nov TABLE(
custCode VARCHAR(10),
PromiseAvg decimal(18,8),
ShipAvg decimal(18,8) )
或任何适合您的比例和精度。我刚刚在SQL Server 2008上进行了测试,如果在decimal(18,0)
语句中未指定create table
,则cast
似乎是默认值。
对...
isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(PromiseDate, 0))
As decimal(18,8))),0) As PromiseAvg,
....
{{1}}