Skip to content

Instantly share code, notes, and snippets.

@andy-uq
Created February 2, 2012 21:16
Show Gist options
  • Save andy-uq/1725825 to your computer and use it in GitHub Desktop.
Save andy-uq/1725825 to your computer and use it in GitHub Desktop.
Returns the umbraco node tree and all descendants
DECLARE @nodeId uniqueidentifier
SELECT @nodeId = 0 -- Put your node Id here
WITH NodeTree(ID, ParentID, [Level])
AS
(
-- The immediate Node we're getting (which will be excluded in the final query)
SELECT a.ID, a.ParentID, 0
FROM Node a
WHERE ID = @nodeId
UNION ALL
-- Recursively retrieve the children
SELECT a.ID, a.ParentID, [Level] + 1
FROM Node a
INNER JOIN NodeTree p ON a.ParentID = p.ID
)
SELECT a.*
FROM [Node] a
INNER JOIN NodeTree tree ON a.ID = tree.ID
WHERE
a.ID <> @nodeId -- Delete the where clause to include the node within the results
ORDER BY tree.[Level], a.sortOrder, a.[text]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment