时间:2021-07-01 10:21:17 帮助过:14人阅读
DECLARE @DiGui TABLE ( Id INT, ParentId INT ) INSERT INTO @DiGui ( Id , ParentId ) SELECT 1 , 2 UNION SELECT 2 , 3 UNION SELECT 3 , 4 UNION SELECT 5 , 10 UNION SELECT 1 , 10 SELECT * FROM @DiGui ; WITH temp ( [Id], [parentid] ) AS ( SELECT Id , parentid FROM @DiGui WHERE [Id] = 1 UNION ALL SELECT a.Id , a.parentid FROM @DiGui a INNER JOIN temp ON a.[Id] = temp.[parentid] ) SELECT * FROM temp
结果:
T-SQL使用CTE递归
标签:dig blog img rom insert from 递归 union logs