I'm trying to create a query similar to this
SELECT
'foo' AS Detail,
'bar' AS Detail,
'baz' AS Detail
FOR XML PATH ('Header')
To produce something like this-
<Header>
<Detail type="A">foo</Detail>
<Detail type="B">bar</Detail>
<Detail type="C">baz</Detail>
</Header>
and it seems like there's no way to create attributes without assigning column values into them.
Best Answer
The query you have
concatenates the values to a single
Detail
element.To have multiple
Detail
elements you need to separate the columns with anull
.And then you use the
@
syntax suggested in the answer by Rob Farley to get the result you are looking for.