在 SQL Server 2017 之前实现字符串聚合
对于使用 SQL Server 2014 或更早版本并希望像示例查询那样连接字符串的用户:
<code class="language-sql">select string_agg(t.id,',') AS id from Table t</code>
以下是您可以针对您的环境调整此查询的方法:
<code class="language-sql">select stuff( (select ',' + cast(t.id as varchar(max)) from tabel t for xml path ('') ), 1, 1, '' );</code>
在这个查询中,stuff()
函数仅用于移除开头的逗号。实际的字符串连接是使用 for xml path
完成的。
以上是2017年之前的SQL Server如何实现字符串聚合?的详细内容。更多信息请关注PHP中文网其他相关文章!