Represent related tables in Azure SQL in XML format(defined in XSD schema)

Partha Das 286 Reputation points
2022-08-24T14:36:58.83+00:00

Lets take Nortwind DB .

We'll have a XSD schema of the DB with all the relationships.
The design of the table looks like:
234526-db-design.jpg

using below script,

========================

SELECT OrderID, CustomerID, EmployeeID,
(select OrderDetails.OrderID, ProductID, UnitPrice, Quantity
from OrderDetails
WHERE OrderDetails.OrderID = Orders.OrderID FOR XML AUTO, ELEMENTS)
from Orders FOR XML AUTO, ELEMENTS;

========================

I got attached output234545-result.xml

Through scripts we can create nested xmls but that is tough to maintain. Is there any other easy way(without 3rd party software help) to get such nested xmls?

Early help is highly appreciated. We can use sql scripts or Azure Data Factory transformation or any azure services

Azure SQL Database
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yitzhak Khabinsky 27,091 Reputation points
    2022-08-24T15:51:32.353+00:00

    Hi @Partha Das ,

    I would suggest the following for improvements:

    1. Use FOR XML PATH(...) ... option, It is most powerful and flexible clause to create an XML.
    2. It is possible to use XQuery's FLWOR expression to fine tune the final XML.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.