递归

with temp ( [Id], [parentid])
as
(
select Id, parentid
from DiGui
where [parentid] = 15
union all
select a.Id, a.parentid
from DiGui a
inner join temp on a.[parentid] = temp.[Id]
)
select * from temp
原文地址:https://www.cnblogs.com/w1-y2-q5/p/7234588.html