时间:2021-07-01 10:21:17 帮助过:66人阅读
;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 ;
SQL 递归查询
标签:sts sele sql with 排除 exist union all 递归查询 select