CTE and FOR XML to generate nested XML
I have an adjacency list in the DB and want to deliver the data in XML format to the client through a SQL SP. I'm trying to use CTE and FOR XML but I am not getting the XML nodes to nest. FYI, this will represent a site map. The Table structure: CREATE TABLE [dbo].[PageHierarchy]( [ModuleId] [int] NOT NULL, [PageId] [int] IDENTITY(1,1) NOT NULL, [ParentPageId] [int] NULL, [PageUrl] [nvarchar](100) NULL, [PageTitle] [nvarchar](50) NOT NULL, [PageOrder] [int] NULL) and the beginnings of the CTE: ;WITH cte AS ( select * from PageHierarchy where ParentPageId is null union all select child.* from