I need a nudge in the right direction here. Simplified data looks like:
ItemId|ItemName|ObjectId|ObjectName
1 | Alpha | 1 | Alpha-1
1 | Alpha | 2 | Alpha-2
2 | Beta | 3 | Beta-1
2 | Beta | 4 | Beta-2
And my target xml needs to be:
<Root>
<Items>
<Item>
<id>1</id>
<name>Alpha</name>
</Item>
<Item>
<id>2</id>
<name>Beta</name>
</Item>
</Items>
<Objects>
<Object>
<id>1</id>
<item_id>1</item_id>
<name>Alpha-1</name>
</Object>
<Object>
<id>2</id>
<item_id>1</item_id>
<name>Alpha-2</name>
</Object>
<Object>
<id>3</id>
<item_id>2</item_id>
<name>Beta-1</name>
</Object>
<Object>
<id>4</id>
<item_id>2</item_id>
<name>Beta-2</name>
</Object>
</Objects>
</Root>
I am having trouble with the FOR XML statement syntax that will allow this kind of break out between the Items section and the Objects section. I know how to code each section individually (the Objects one anyway), but do not know how to get this stacked output. Can I do this from the single View shown here or do I need split things up and them splice the xml back together somehow?
Yes, you need to “split things up and then splice the xml back together somehow?”. You need two queries, one for Items and one for Objects.
Here is a way to do just that.