当计数为0时,更新为0不显示空

时间:2015-07-13 14:51:29

标签: sql-server-2008-r2

我正在运行此更新查询,我希望它会更新表以保持值为0而不是在计数为0时显示NULL。如何更新以强制表,因此在显示时显示0返回的计数是0?

set @sql = 
  'UPDATE cra
  SET mastercounts = te.LC
  FROM #tbl_test As cra
  JOIN (
        select 
        ,COUNT(CASE WHEN recordID < 1 THEN 0 ELSE recordID end) As LC
        ,employeename
        from productioninformaiton
        where salestatus = (''Approved'', ''Shipped'')
        Group By employeename
  ) As te 
  ON cra.[StoreName] = te.[StoreName]
  AND cra.[employeename] = te.[employeename]'
Exec (@sql)

1 个答案:

答案 0 :(得分:1)

也许是字段&#34; recordID&#34;在某些情况下返回NULL,这使得COUNT(NULL)也返回NULL。尝试替换

COUNT(CASE WHEN recordID < 1 THEN 0 ELSE recordID end) As LC

COUNT(CASE WHEN ISNULL(recordID, 0) < 1 THEN 0 ELSE ISNULL(recordID, 0) end) As LC