sql server 2008中表中的子节点 [英] sub node in a table in sql server 2008

查看:99
本文介绍了sql server 2008中表中的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2
down vote
favorite
I have structure like this:


<Unit>
  <SubUnit1>
           <SubSubUnit1/>
           <SubSubUnit2/>
           ...
           <SubSubUnitN/>
  </SubUnit1/>
  <SubUnit2>
           <SubSubUnit1/>
           <SubSubUnit2/>
           ...
           <SubSubUnitN/>
  </SubUnit2/>
  ...
  <SubUnitN>
           <SubSubUnit1/>
           <SubSubUnit2/>
           ...
           <SubSubUnitN/>
  </SubUnitN/>
</Unit>
This structure has 3 levels: main Unit, SubUnits and SubSubUnits.

I want to select all children by UnitId.
If I search by Unit, I have to get all tree.
If I search by SubUnit1, I have to get SubUnit1 and all children of SubUnit1.
If I search SubSubUnit2, I have to get itself.

推荐答案

考虑使用递归CTE。它将返回一个包含所有子项的行的表。详情请见:http://stackoverflow.com/questions/14274942/sql-server-cte-and-recursion-example
Consider using recursive CTE for that. It will return a table with rows containing all children. See here for details: http://stackoverflow.com/questions/14274942/sql-server-cte-and-recursion-example


这篇关于sql server 2008中表中的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆