;WITH T AS
(
SELECT Id
FROM tb AS A
WHERE
--NOT EXISTS(SELECT * FROM tb WHERE Id = A.ParentId ) AND --加这句条件,就排除自己
A.Id='5801CB7F-06DB-46A7-BA97-F0C7CB5D3ADA'
UNION ALL
SELECT A.Id FROM [tb] AS A JOIN T AS B ON A.ParentId = B.Id
)
SELECT Id FROM T ;