Query to get XML output for hierarchical data using FOR XML PATH in SQL Server
-
I have a table with columns NodeId, NodeName, ParentNodeId and I want to ouput entire table data in the form of Xml like the following using SQL query. I think FOR XML PATH mode in Sql server can be used to achieve this (I use SQL Server 2008) using recursion, but not sure how. Thanks in advance <?xml version="1.0" encoding="utf-8" ?> <Nodes> <Node Id="1" Name="node1"> <Node Id="11" Name="node11"> <Node Id="111" Name="node111" /> <Node Id="112" Name="node112" /> </Node> </Node> <Node Id="2" Name="node2"> <Node Id="21" Name="node21"> <Node Id="211" Name="node211" /> <Node Id="212" Name="node212" /> </Node> </Node> </Nodes>
-
Answer:
I solved it using a stored procedure and a recursive function. code shown below. (actually I wanted this to generate a menu xml, so the code is shown for the menu. CREATE PROCEDURE [dbo].[usp_GetMenu] AS BEGIN SET NOCOUNT ON; SELECT dbo.fnGetMenuItems(MenuId) FROM dbo.Menu WHERE ParentMenuId IS NULL FOR XML PATH('MenuItems') END GO CREATE FUNCTION [dbo].[fnGetMenuItems] ( @MenuId int ) RETURNS XML WITH RETURNS NULL ON NULL INPUT AS BEGIN RETURN ( SELECT MenuId AS "@Id" , [Name] AS "@Name" , [URL] AS "@URL" , [Key] AS "@Key" , [dbo].[fnGetMenuItems](MenuId) FROM dbo.Menu WHERE ParentMenuId = @MenuId FOR XML PATH('MenuItem'),TYPE ) END GO
RKP at Stack Overflow Visit the source
Other answers
This query will do it - however, it's not very clean in that you have to "manually" define the nesting and it won't just automatically scale to more levels deep.... SELECT n.ID AS '@Id', n.NAME AS '@Name', (SELECT n2.ID AS '@Id', n2.NAME AS '@Name', (SELECT n3.ID AS '@Id', n3.NAME AS '@Name' FROM dbo.Nodes n3 WHERE n3.ParentNode = n2.ID FOR XML PATH('Node'), TYPE ) FROM dbo.Nodes n2 WHERE n2.ParentNode = n.ID FOR XML PATH('Node'), TYPE ) FROM dbo.Nodes n WHERE n.ParentNode IS NULL FOR XML PATH('Node'), ROOT('Nodes') Output is: <Nodes> <Node Id="1" Name="node1"> <Node Id="11" Name="node11"> <Node Id="111" Name="node111" /> <Node Id="112" Name="node112" /> </Node> </Node> <Node Id="2" Name="node2"> <Node Id="21" Name="node21"> <Node Id="211" Name="node211" /> <Node Id="212" Name="node212" /> </Node> </Node> </Nodes> I was hoping there would be a way to do this with a recursive CTE (Common Table Expression), but that didn't work out :-(
marc_s
Related Q & A:
- How to present large dataset from a SQL Server query?Best solution by stackoverflow.com
- How to output the results of an procedure in SQL server?Best solution by Stack Overflow
- How to import XML into SQL Server database?Best solution by Stack Overflow
- How to change column data's as a separate column wise format in a SQL Server?Best solution by stackoverflow.com
- How do I connect to SQL Server using C#?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.