Попробуйте это ниже. Рекурсивный CTE
DECLARE @TABLE AS TABLE (RoutineID INT, DependentRoutineID INT)
INSERT INTO @TABLE
SELECT 1 , NULL UNION ALL
SELECT 2 , 1 UNION ALL
SELECT 3 , 2 UNION ALL
SELECT 4 , 3 UNION ALL
SELECT 5 , 3 UNION ALL
SELECT 6 , 4 UNION ALL
SELECT 7 , 5 UNION ALL
SELECT 8 , NULL UNION ALL
SELECT 9 , 8 UNION ALL
SELECT 10, 9
;WITH CTE
AS
(
SELECT RoutineID,
DependentRoutineID AS DependentRoutineIDDup,
RoutineID As DependentRoutineID1
FROM @TABLE
WHERE DependentRoutineID IS NULL
UNION ALL
SELECT t.RoutineID,
t.DependentRoutineID,
DependentRoutineID1
FROM CTE c
INNER JOIN @TABLE t
ON c.RoutineID = t.DependentRoutineID
)
SELECT RoutineID,
CASE WHEN DependentRoutineIDDup IS NULL THEN NULL ELSE DependentRoutineID1 END DependentRoutineID
FROM CTE
ORDER BY RoutineID
Результат
RoutineID DependentRoutineID
------------------------------
1 NULL
2 1
3 1
4 1
5 1
6 1
7 1
8 NULL
9 8
10 8