在SQL中获取与Parent相关的所有节点

时间:2016-03-22 09:15:33

标签: sql sql-server-2008

我有表Acc_Region

enter image description here

我需要选择与Parent -Delta Sector相关的所有节点 - 当我使用查询

select  Name from Acc_Region where ID = 1

获取所有引用

Cairo
Faysl City
Helwan City
Giza
Aiat
Saf

我尝试使用:

WITH RECURSIVE Region (ID, name, Parent_ID) AS
(
    SELECT ID, 
           name, 
           Parent_ID
    FROM ACC_REGION 
    UNION ALL
    SELECT Acc.ID, 
           Acc.name,
           Acc.Parent_ID
    FROM ACC_REGION Acc
      JOIN Region Reg ON Acc.userid = Reg.managerid
)
SELECT *FROM ACC_REGION

但是RECURSIVE存在问题。

有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

这就是应该如何构造递归CTE:

<div id="gauge" class="400x260px"></div>

<!---The Form--->

<form class="form-inline" action="" id="myform" form="" method="post">
<!-- Text input-->
<div class="form-group">
    <label class="col-md-4 control-label" for="total"></label>
    <div class="col-md-8">
        <input id="total" name="total" type="text" placeholder="total" class="form-control input-lg" required>
    </div>
</div>
<!-- Button -->
<div class="form-group">
    <label class="col-md-4 control-label" for="submit1"></label>
    <div class="col-md-4">
        <button id="submitButtonId" name="submit1" class="btn btn-primary btn-xl">Submit</button>
    </div>
</div>
</form>