I am working on retrieving data in Xml format from SQL Server 05 using FOR XML
What is the best practice for nesting elements in my resulting Xml?
Currently I am doing this:
Select
(
Select
[Col1] As [Col1],
[Col2] As [Col2]
From [dbo].[NestedTable] As T1
Where T0.[Key] = T1.[Key]
FOR XML PATH('NestedTable'), TYPE
),
[Col1] As [Col1],
[Col2] As [Col2],
From [dbo].[TopLevelTable] As T0
FOR XML PATH('TopLevelTable'), ROOT('TopLevelTableItems')
However, I am concerned about the performance of this due to the correlated sub query. Is there Group By type functionality that I can use so that I can do a regular inner join on the two tables and have resulting Xml where the top level nodes are unique and have the appropriate collection of child nodes?
Edit It seems like I may be asking for the impossible given the lack of responses. So perhaps a better question is:
Is there a good / better way to be generating Xml from SQL Server or should I just shift my code out into the CLR and generate from there?
Correlated sub-queries should be avoided if possible (not a big deal if the tables are guaranteed to be small).
Perhaps you should first define the XML schema (presumably something is consuming this and expects a certain format).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.