with temp_Dept as(
select DepartID,DepartName,ParentDeptID from Dept where ParentDeptID is NULL
union all
select A.DepartID,A.DepartName,A.ParentDeptID from Dept a join temp_Dept b on a.ParentDeptID=b.DepartID
)
select * from temp_Dept
PS:通过判断ParentDeptID和DepartID是否相等判断是否是上下级关系