declare @tb table(id int)
insert @tb select sonid from test where fatherid = 1
while @@rowcount > 0
insert @tb select a.sonid from test as a
inner join @tb as b on a.fatherid = b.id and a.sonid not in(select id from @tb)
select * from @tb
select sonid from test where sonid not in(select id from @tb)