Example code for you
select * from (
select 1 as id,'one' as name
union all
select 2 as id,'two' as name
union all
select 3 as id,'three' as name) as a
for xml path('units'),ROOT('root')
Result XML
<root>
<units>
<id>1</id>
<name>one</name>
</units>
<units>
<id>2</id>
<name>two</name>
</units>
<units>
<id>3</id>
<name>three</name>
</units>
</root>
Explanation : Convert Sql Select To XML
The Keyword used to convert an sql select into xml is "for xml path(),ROOT()" the parameter which goes inside the path() eg. path('units')is the tag which wraps the every single record. And the parameter which goes inside the ROOT() eg.,ROOT('root')is the one one which forms the root xml tag, i.e it wraps all the records as a whole.
No comments:
Post a Comment