开发者

Is this select possible in sql?

开发者 https://www.devze.com 2023-01-01 03:52 出处:网络
Consider a table, Id columnA 1a 2b 3c Select ColumnA from table gives the result as below, columnA a b c Is it possible to get

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

精彩评论

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