How can I grab just part of an xml column, and return it as it’s own result set in SQL?
I have an xml file in my database that looks like this:
<main>
<data>
<rows>
<_items>
<columns>
<values>
<keyvaluepairs>
<keyvaluepairofstringstring>
<key>EVENT</key>
<value>Wedding</value>
</keyvaluepairofstringstring>
<keyvaluepairofstringstring>
<key>DETAIL</key>
<value>Smith-Wesson</value>
</keyvaluepairofstringstring>
</keyvaluepairs>
</values>
</columns>
<columns>
<values>
<keyvaluepairs>
<keyvaluepairofstringstring>
<key>EVENT</key>
<value>Reunion</value>
</keyvaluepairofstringstring>
<keyvaluepairofstringstring>
<key>DETAIL</key>
<value>Class of 1996</value>
</keyvaluepairofstringstring>
</keyvaluepairs>
</values>
</columns>
<columns>
<values>
<keyvaluepairs>
<keyvaluepairofstringstring>
<key>EVENT</key>
<value>Pie-throwing contest</value>
</keyvaluepairofstringstring>
<keyvaluepairofstringstring>
<key>DETAIL</key>
<value>Cherry pies</value>
</keyvaluepairofstringstring>
</keyvaluepairs>
</values>
</columns>
</_items>
</rows>
</data>
</main>
And I want to return the “rows” as a result set like this:
EVENT NAME
Wedding Smith-Wesson
Reunion Class of 1996
Pie-throwing contest Cherry pies
...etc...
I am using the method described here to query out individual values, but that isn’t enough to get all the data I really need.
I’m using SQL Server 2008.
Edit: To clarify, this xml is contained in a single row. For example, to return the above xml, I would type
select my_xml_column from my_table;
try this:
Test here: https://data.stackexchange.com/stackoverflow/q/122873/
BTW, I had to fix your XML. There where some missing end tags.