--查找某節點的所有子節點(包含自己)
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