If I have a parent table and a child table where there can be one parent to many children, how can I go about returning the following xml from a stored procedure in a stored procedure?
<Parents>
<Parent>
<ID>Integer</ID>
<Children>
<Child>
<ID>Integer</ID>
<Text>String</Text>
</Child>
<Child>
<ID>Integer</ID>
<Text>String</Text>
</Child>
</Children>
</Parent>
<Parent>
<ID>Integer</ID>
<Children>
<Child>
<ID>Integer</ID>
<Text>String</Text>
</Child>
<Child>
<ID>Integer</ID>
<Text>String</Text>
</Child>
</Children>
</Parent>
</Parents>
This is definitely a lot harder with SQL 2000. Below is a sample query that may help you get started with this process. Please understand that it’s not exactly in the format you are looking for. The purpose of the query is to help you get started… a nudge in the right direction.
The trick here is to use FOR XML EXPLICIT, and carefully crafting your column aliases to control the positioning of the elements.