I have been given a table with the following columns and some example data:
ID Title FieldsXml [nvarchar(max)]
-- ----- -------------------------
1 A <Fields><Field Name="X">x1</Field><Field Name="Y">y1</Field></Fields>
2 B <Fields><Field Name="Y">y2</Field><Field Name="Z">z2</Field></Fields>
3 C <Fields><Field Name="Z">z3</Field></Fields>
And I need to query it to get a result like this:
ID Title X Y Z
-- ----- -- -- --
1 A x1 y1
2 B y2 z2
3 C z3
The xml field is supposedly guaranteed to be well formed and match the schema even though it is an nvarchar and not xml typed. However, the values of the Name attribute are not known ahead of time.
I am using SQL Server 2008. I can use a stored proc if necessary, but am looking for a solution that can avoid that and also avoid dynamic SQL. Is it impossible to write such a query?
If stored procs or dynamic sql are the only way, I am open to a solution using that.