SQL, How to Concatenate results?

前端 未结 7 1931
渐次进展
渐次进展 2020-11-29 08:08

I currently have a SQL query that returns a number of fields. I need one f the fields to be effectively a sub query sub that.

The Problem in detail:

7条回答
  •  -上瘾入骨i
    2020-11-29 08:35

    This one automatically excludes the trailing comma, unlike most of the other answers.

    DECLARE @csv VARCHAR(1000)
    
    SELECT @csv = COALESCE(@csv + ',', '') + ModuleValue
    FROM Table_X
    WHERE ModuleID = @ModuleID
    

    (If the ModuleValue column isn't already a string type then you might need to cast it to a VARCHAR.)

提交回复
热议问题