使用表函數查找某節點的所有子節點

--查找某節點的所有子節點(包含自己)

alter function rerr(@dep int)

returns @table table (TDtID int )

as

begin

 insert into @table

 select tdtid from TDept where tdtpid=@dep;

begin

 while @@rowcount >0

 

    insert @table

    select a.tdtid from TDept a inner join @table  b on a.tdtpid=b.tdtid  and a.tdtid  not in (select * from @table)

      --子級的父級部門ID=父級的部門ID

 end

    insert @table

    select tdtid from TDept where tdtid =7; --包含自己就加上這一句話

return

end
發佈了41 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章