开发者

Is this select possible in sql?

Consider a table,

Id columnA
1  a
2  b
3  c

Select ColumnA from table gives the result as below,

columnA
   a
   b
   c

Is it possible to get

ColumnA
a,b,c
开发者_StackOverflow中文版


One way is the XML PATH trick

SELECT
    SUBSTRING(
    (
    SELECT
        ',' + columnA
    FROM
        myTable
    FOR XML PATH ('')
    )
     , 2, 7999)
FROM
     foo


heres an article describing how to do it with a stored procedure which internally uses a loop to do the concatenation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜