-2
A
回答
0
您是否嘗試過使用遞歸CTE? 例如: 假設你有表TBL(EMPID,名稱,MngrId),它具有自引用關係
create table tbl
(
EmpId int not null,
Name nvarchar(100),
MngrId int null)
insert into tbl(EmpId, Name, MngrId)
values (1,'Adel',Null),
(2,'Ali',1),
(3,'Shaban',1),
(4,'Mark',3),
(5,'John',3),
(6,'Tony',Null),
(7,'Peter',6)
您可以創建這樣一些觀點:
create view Employees
Begin
with cte
as
(
Select EmpId,Name, Null as MngrId, cast(null as nvarchar(100)) as MngrName, 1 as EmpLevel
from tbl
where MngrId is Null
Union All
Select t.EmpId, t.Name, c.EmpId as MngrId, c.Name as MngrName, c.EmpLevel + 1 as EmpLevel
from tbl t
inner join cte c
on t.MngrId = c.EmpId
)
Select *
from cte
order by EmpLevel, EmpId
End
您現在可以使用EmpLevel
跳轉到不同的級別MngrName
得到關於父節點的信息
相關問題
- 1. TSQL:層次結構從上到下的總和
- 2. 沒有爲此實體層次結構定義的ID
- 3. 父類和子類的層次結構
- 4. 父/子類層次結構中的NodeJS
- 5. Tomcat的上下文層次結構
- 6. 本書的上下文層次結構
- 7. mondrian中的多層次結構說沒有找到層次結構
- 8. 沒有云的PowerBI層次結構
- 9. CSS層次結構類和ID
- 10. 要上線和下線的數據進行特定的ID
- 11. Espresso:層次結構中沒有視圖與id匹配:android:id/home
- 12. &&和||的層次結構
- 13. Android的層次和結構
- 14. 確定剃刀網頁層次結構
- 15. 如何檢查Swift/Xcode中的線程/隊列層次結構?
- 16. Spring上下文層次結構
- 17. 如何在沒有層次結構的情況下遵循xpath?
- 18. 父子層次結構中的遞歸總和T-SQL
- 19. 您有以下類層次結構
- 20. 當id和父母id在同一個表中時,我可以選擇父母的完整層次結構嗎?
- 21. 在SQL Server中具有多個父項的層次結構
- 22. PHP類層次結構 - 層次結構中的最高類
- 23. Python包中__init__.py層次結構的正確結構
- 24. 圖中的層次結構
- 25. CheckBoxList中的層次結構
- 26. MVC3中的層次結構
- 27. SQL和ASP.net中的層次結構
- 28. 從父子層次結構表中獲取頂級父ID身份證
- 29. 沒有實例共享變量的複合和層次結構
- 30. 查詢訂購父/子-層次結構
如果你想得到任何有用的答案,你的問題就非常模糊了,需要更多的細節。至少應該提供表格模式和一些樣品數據,以及您的預期結果應該基於樣品數據。 – 2014-09-24 13:25:08