Skip to content
Advertisement

How to add additional XML node on top of my SQL generated XML

I have generated XML from a SQL Server FOR XML PATH statement as shown here:

And this is the result:

Now, I’d like to encapsulate the above with these nodes:

How will I achieve this in a SQL Query I created above?

I am new to SQL Server and XML. I am trying to generate this XML directly from my database and vice versa to make it more efficient and faster — let my SQL directly communicate with XML.

ATTEMPT 1:

ATTEMPT 2:

Advertisement

Answer

There are many ways to construct your XML result, consider the following three alternatives…

Use XML.modify() to insert the BillTable XML into an XML scalar variable (which includes the ?qbxml XML processing instruction):

Use a nested query to construct the entire XML result (which does not, however, include the ?qbxml XML processing instruction):

Or use an XQuery to construct the entire XML result (which also includes the ?qbxml XML processing instruction):

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement