这里有很多的。楼主搜索一下。
--创建处理函数,查询指定id的所有子结点,同时包含传入的id节点。楼主可适当修改,以供自己需要。
create function f_id(@id int)
returns @re table(NodeID int,level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.NodeID,@l from Tree a join @re b on a.ParentNodeID=b.NodeID  where b.level=@l-1
end
return
end