sql server 递归查询树型结构某节点的所有上级节点,并且把这些所有上级节点多行拼接为一行,即合并列数据
with eps_root(pk_eps, pk_parent, eps_code, eps_name) as ( SELECT pk_eps, pk_parent, eps_code, eps_name FROM pm_eps where enablestate = 2 and pk_eps = '1001A11000000003P62E' union all SELECT e.pk_eps, e.pk_parent, e.eps_code, e.eps_name FROM pm_eps e inner join eps_root r on e.pk_eps = r.pk_parent where e.enablestate = 2 ) select distinct eps_code = stuff((select '/' + convert(nvarchar(500), eps_code) from (select distinct eps_code from eps_root) r1 for xml path('')), 1, 1, ''), --此次需要使用eps_code排序,不然中文的会错乱顺序 eps_name = stuff((select '/' + eps_name from (select top 99.99 PERCENT eps_name from eps_root order by eps_code) r2 for xml path('')), 1, 1, '') from eps_root
执行sql,如下图效果:
到此这篇关于sqlserver 合并列数据的实现的文章就介绍到这了,更多相关sqlserver 合并列数据内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!