with s( id, pid, name, value) as
(
select f.id, f.pid, cast(f.name as nvarchar) as name, f.value from foos f
where f.pid is null
union all
select f.id, f.pid, cast(s.name + f.name as nvarchar) as name, f.value from foos f inner join s on f.pid = s.id
)
select * from s
where not exists(select * from foos where pid = s.id)