SQL 2005 generates XML comment, array of elements

By admin - Last updated: Monday, November 12, 2007 - Save & Share - Leave a Comment
SELECT
        'nameValue' AS "@name",
        CAST('<!-- your comment -->' AS XML),
        'anotherValue' AS "another",
        (SELECT 'i' AS "@name", '1' AS "@value" FOR XML PATH('item'), TYPE),
        (SELECT 'j' AS "@name", '2' AS "@value" FOR XML PATH('item'), TYPE)
FOR XML PATH('sample'), ROOT('root')

output

<root>
  <sample name="nameValue">
    <!-- your comment -->
    <another>anotherValue</another>
    <item name="i" value="1"></item>
    <item name="j" value="2"></item>
  </sample>
</root>



Posted in SQL • Tags: , , , Top Of Page