patternsqlMinor
Is there a FOR XML variant that can add attributes with arbritrary values?
Viewed 0 times
canvariantwithxmlattributesthatforvaluestherearbritrary
Problem
I'm trying to create a query similar to this
To produce something like this-
and it seems like there's no way to create attributes without assigning column values into them.
SELECT
'foo' AS Detail,
'bar' AS Detail,
'baz' AS Detail
FOR XML PATH ('Header')To produce something like this-
foo
bar
baz
and it seems like there's no way to create attributes without assigning column values into them.
Solution
The query you have
concatenates the values to a single
To have multiple
And then you use the
SELECT
'foo' AS Detail,
'bar' AS Detail,
'baz' AS Detail
FOR XML PATH ('Header')
concatenates the values to a single
Detail element.
foobarbaz
To have multiple
Detail elements you need to separate the columns with a null.SELECT
'foo' AS Detail,
null,
'bar' AS Detail,
null,
'baz' AS Detail
FOR XML PATH ('Header')
foo
bar
baz
And then you use the
@ syntax suggested in the answer by Rob Farley to get the result you are looking for.SELECT
'A' AS 'Detail/@type',
'foo' AS Detail,
null,
'B' AS 'Detail/@type',
'bar' AS Detail,
null,
'C' AS 'Detail/@type',
'baz' AS Detail
FOR XML PATH ('Header')
foo
bar
baz
Context
StackExchange Database Administrators Q#120375, answer score: 6
Revisions (0)
No revisions yet.