如何在Stuff函数中包含NULL值?

时间:2018-05-11 07:08:51

标签: sql sql-server

我正在使用表格开发一个Sql视图,使用Stuff函数将结果分隔为每个唯一ID的逗号。但在许多情况下,id具有NULL值,而另一行中的相同id具有一些通用值。最后,视图显示两行,同一个id为NULL,另一行为逗号分隔值。我只想要一排。怎么做到这一点?以下是我的理解代码:

    CREATE VIEW BM_RLOS_VW_RCO_AUTH
AS
(SELECT DISTINCT
A.bpm_referenceno AS ref_no, 

STUFF((SELECT ', '+B.takenby
FROM BM_RLOS_DecisionHistoryForm B 
WHERE A.winame ='RCO' AND A.bpm_referenceno=B.bpm_referenceno FOR XML PATH(''),Type).value('.','nvarchar(max)'),1,2,'') takenby
FROM BM_RLOS_DecisionHistoryForm A 
)

1 个答案:

答案 0 :(得分:0)

检查列中的ISNULL

  CREATE VIEW BM_RLOS_VW_RCO_AUTH
  AS
   (
      SELECT DISTINCT A.bpm_referenceno AS ref_no, 
             STUFF((SELECT ', '+ ISNULL( B.takenby ,'' )
      FROM BM_RLOS_DecisionHistoryForm B 
      WHERE A.winame ='RCO' AND A.bpm_referenceno=B.bpm_referenceno FOR XML 
      PATH(''),Type).value('.','nvarchar(max)'),1,2,'') takenby
      FROM BM_RLOS_DecisionHistoryForm A 
    )