Skip to content

Instantly share code, notes, and snippets.

@CodyEngel
Created January 27, 2015 20:21
Show Gist options
  • Save CodyEngel/fb4ad57db2d4e1535442 to your computer and use it in GitHub Desktop.
Save CodyEngel/fb4ad57db2d4e1535442 to your computer and use it in GitHub Desktop.
Get All Child Nodes Given A Parent
DECLARE @TopID_in int -- The top level we want to resolve children for
SELECT @TopID_in = -- ID of parent node
;WITH HierarchyCTE(ID, ParentID) AS
(
SELECT Id, ParentId
FROM Categories
WHERE Id = @TopID_in
UNION ALL
SELECT Categories.Id, Categories.ParentId
FROM Categories
INNER JOIN HierarchyCTE
ON Categories.ParentId = HierarchyCTE.Id
)
SELECT *
FROM HierarchyCTE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment