OK. So I’m finally returning to a SQL Server topic. The integration tool I’m working on does a great deal of processing of XML. In some cases, a single XML document consists of multiple result sets. I won’t get into the details of why we need this, but a good example of where you might use it is to return an Order object from a database for an edit form and additional return a list of Order Types to populate the Order Type drop down box. In this case the XML might look like this:
<orderformdata>
<orders>
<order>
<orderdate>1/1/2011</orderdate>
<ordertypeid>1</ordertypeid>
<orderamount>1000.00</orderamount>
</order>
<order>
<orderdate>1/1/2011</orderdate>
<ordertypeid>1</ordertypeid>
<orderamount>1000.00</orderamount>
</order>
</orders>
<references>
<ordertypes>
<ordertype id="1" name="Professional Services"/>
<ordertype id="2" name="Product"/>
<ordertype id="3" name="Support Contract"/>
</ordertypes>
</references>
</orderformdata>
Using SQL Servers FOR XML clause, it’s quite easy to output a result set as XML. With the EXPLICIT directive you can have a great amount of control over how the XML is rendered. However, the examples tend to show how to create XML from a single result set or nested records (say orders and order details). So how to return the above?