Q&A for professional and enthusiast programmers
DECLARE @QuestionId INT = ##relatedpostid##; WITH R AS (SELECT @QuestionId AS PostId, 0 AS Level, CAST(CONCAT('/', @QuestionId, '/') AS VARCHAR(8000)) AS Path UNION ALL SELECT pl.PostId, Level + 1, CONCAT(Path, pl.PostId, '/') FROM PostLinks pl JOIN R ON R.PostId = pl.RelatedPostId WHERE pl.LinkTypeId = 3 AND Path NOT LIKE CONCAT('%/', pl.PostId, '/%')) SELECT PostId AS [Post Link], Level, Path FROM R JOIN Posts p ON p.Id = R.PostId WHERE Level > 0