I love this solution. Very creative. I'm trying to see how it would work to take it a step further. I have xml data in a column and in that xml there are elements I want to number, but also want to extract the child elements also numbered.
So if the XML in the table (we'll call it scheduleInfo) is in a field called scheduleXML, is there a way to make it work.
So one sample record in the xml would look like:
<Schedules>
<Name value="Bob">
<Class>Math</Class>
<Class>English</Class>
<Class>Science</Class>
</Name>
<Name value="Joe">
<Class>History</Class>
<Class>Math</Class>
<Class>English</Class>
</Name>
</Schedules>
What I'd like to get in my output is (position of the name, name, position of the class, class):
1,Bob,1,Math
1,Bob,2,English
1,Bob,3,Science
2,Joe,1,History
2,Joe,2,Math
2,Joe,3,English
Any thoughts on how to do this? Seems I may have to break out the xml into another table to get the position, name and then the xml of the classes, and then possibly go from there? Very new to XML in SQL Server so trying to wrap my head around this. I couldn't even get this solution properly written to pull from a table instead of a variable (may have just missed something) so going further down is really twisting my brain.
Thanks
commented on Aug 29 2012 12:50AM