How Stuff and 'For Xml Path' work in Sql Server

后端 未结 9 1351
盖世英雄少女心
盖世英雄少女心 2020-11-21 13:49

Table is:

+----+------+
| Id | Name |
+----+------+    
| 1  | aaa  |
| 1  | bbb  |
| 1  | ccc  |
| 1  | ffffd  |
| 1  | eee  |
+----+------+

9条回答
  •  太阳男子
    2020-11-21 14:07

    There is very new functionality in Azure SQL Database and SQL Server (starting with 2017) to handle this exact scenario. I believe this would serve as a native official method for what you are trying to accomplish with the XML/STUFF method. Example:

    select id, STRING_AGG(name, ',') as abc
    from temp1
    group by id
    

    STRING_AGG - https://msdn.microsoft.com/en-us/library/mt790580.aspx

    EDIT: When I originally posted this I made mention of SQL Server 2016 as I thought I saw that on a potential feature that was to be included. Either I remembered that incorrectly or something changed, thanks for the suggested edit fixing the version. Also, pretty impressed and wasn't fully aware of the multi-step review process that just pulled me in for a final option.

提交回复
热议问题