我试图让简单的 SQL Server 2008 递归查询工作。
按照这些示例: http: //msdn.microsoft.com/en-us/library/ms186243.aspx 和 SQL Server 递归查询
我有一个表,带有 id 和 parentID:
ID fParent fName
2 NULL root
3 2 Drug_Error
4 2 Incident
5 4 2007
6 4 2009
7 5 2007-1
8 7 2008-2
使用以下查询
with recury as (
Select
fs1.ID ,fs1.FParent,fs1.FName
from FoldersStructure as fs1
where fs1.FParent =null
union all
select fs2.id,fs2.FParent,fs2.FName
from FoldersStructure as fs2
inner join recury as r on fs2.FParent= r.ID
)
select ID,FParent,FName
from recury
where ID=8
我希望得到:
2 null root
4 2 incident
5 4 2007
7 5 2007-1
8 7 2007-2
但我只得到最后一个。提前致谢。