With a sample table
create table tbl (departmentid int, departmentidparent int) insert tbl select 13,13 insert tbl select 14,13 insert tbl select 15,13 insert tbl select 16,14 insert tbl select 17,11 insert tbl select 115,17
This is a request that will do what you need.
;with cte as ( select * from tbl where departmentid=13 union all select tbl.* from tbl join cte on tbl.departmentidparent=cte.departmentid
source share