Skip to content

Instantly share code, notes, and snippets.

@kwestground
Created November 19, 2021 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwestground/2a3df21e73c225eaa22f75b7a9c0bdda to your computer and use it in GitHub Desktop.
Save kwestground/2a3df21e73c225eaa22f75b7a9c0bdda to your computer and use it in GitHub Desktop.
WITH TreeRecursive AS
(
SELECT T0.ParentCode, T0.ChildCode, 0 [Level] FROM TreeTable T0
WHERE ParentCode = 'ParentCode'
UNION ALL
SELECT TS.ChildCode, T0.ChildCode, TS.[Level] + 1 AS [Level] FROM TreeTable TS
INNER JOIN TreeTable T0 ON TS.ChildCode = T0.ParentCode
)
SELECT * FROM TreeRecursive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment