What this query does to create comma delimited list SQL Server?

前端 未结 4 1585
轮回少年
轮回少年 2020-11-22 11:39

I\'ve written this query with the help of google to create a delimited list from a table but I didn\'t understand anything from this query.

Can anyone explain me wha

4条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 12:05

    SQL Server 2017 makes this much easier with the new STRING_AGG. Recently came across this post and switched my STUFF/FOR XML strategy to use the new string function. Also avoids the need to do an extra JOIN/SUBQUERYand the overhead of FOR XML (and the odd-encoding issues) and hard to interpret SQL.

    SELECT  E1.deptno, 
            STRING_AGG(E1.ename, ', ') AS allemp
    FROM    EMP AS e1 
    GROUP BY DEPTNO; 
    

    Note: Also be sure to check out the counterpart STRING_SPLIT to make working with SQL delimited data much easier.

提交回复
热议问题