Paste this into a new question or an answer at dba.stackexchange.com:
<!-- --> > CREATE TABLE [dependence]( [id] [int] IDENTITY(1,1) NOT NULL, > [name] [nvarchar](255) NULL, > [task_uid] [int] NULL, > [link_pred_uid] [int] NULL, > [link_uid] [int] NULL, > [link_succ_uid] [int] NULL); > > INSERT INTO [dependence] ([name],[task_uid],[link_pred_uid],[link_uid],[link_succ_uid]) > VALUES > ('task 1',10,4,2,2), > ('task 2',11,6,4,2), > ('task 3',12,6,6,4), > ('task 4',13,10,8,8), > ('task 5',14,10,10,8); > > SELECT * FROM dependence; > > WITH cte AS ( SELECT *, > CAST(link_uid AS VARCHAR(64)) path > FROM dependence > WHERE link_uid = link_succ_uid > UNION ALL > SELECT dependence.*, > CAST(CONCAT(cte.path, '/', dependence.link_uid) AS VARCHAR(64)) > FROM dependence > JOIN cte ON cte.link_pred_uid = dependence.link_uid > WHERE cte.link_pred_uid != cte.link_uid ) > SELECT * > FROM cte; > GO > > <pre> > id | name | task_uid | link_pred_uid | link_uid | link_succ_uid > -: | :----- | -------: | ------------: | -------: | ------------: > 1 | task 1 | 10 | 4 | 2 | 2 > 2 | task 2 | 11 | 6 | 4 | 2 > 3 | task 3 | 12 | 6 | 6 | 4 > 4 | task 4 | 13 | 10 | 8 | 8 > 5 | task 5 | 14 | 10 | 10 | 8 > > id | name | task_uid | link_pred_uid | link_uid | link_succ_uid | path > -: | :----- | -------: | ------------: | -------: | ------------: | :---- > 1 | task 1 | 10 | 4 | 2 | 2 | 2 > 4 | task 4 | 13 | 10 | 8 | 8 | 8 > 5 | task 5 | 14 | 10 | 10 | 8 | 8/10 > 2 | task 2 | 11 | 6 | 4 | 2 | 2/4 > 3 | task 3 | 12 | 6 | 6 | 4 | 2/4/6 > </pre> *db<>fiddle [here](https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=f894cb793392de17b26aba47bac22a38)*
back to fiddle