My Blog List

hotinit.com. Powered by Blogger.

Search This Blog

Sql To Xml ( Create xml using sql select )

Tuesday, 26 June 2012

Sql To XML Image



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

 

Blogger news

Blogroll

Most Reading

8.6/10