I have a SQL query that produces one row, with multiple columns from Microsoft SQL Server 2016.
I am trying to produce output in XML format, and can use the SQL XML basic parameters to get variations of:
<FirstName>Fred</FirstName>
<LastName>Smith</LastName>
..
..
..
What I need is:
<Answer name=“FirstName”>
<value>Fred</value>
</Answer>
<Answer name=“LastName”>
<value>Smith</value>
</Answer>
..
..
..
(I realize this may be an inefficient structure for XML - I didn't design the target!)
I can probably achieve what is needed by using column labels / values and a load of explicit replace operations, but before setting off on that path (there are a lot of columns!), I wanted to check in to see if there is a more straightforward approach?
Thanks in advance.
1条答案
按热度按时间wwtsj6pe1#
A minimal reproducible example is not provided. Shooting from the hip.
Notable points:
CROSS APPLY
is producing a generic XML regardless of the table structure.Overall, the answer is following the same minimal reproducible example pattern. You copy it to SSMS as-is, and it is working.
SQL
Output