I have written an interface to interact with the CubeIQ BlackBox (www.magiclogic.com) and I have an SQL Server 2005 database table where I store some regular text fields, dates, and two pieces of XML in columns that have an XML data type.
There are a few values I want to pick out of the XML, and I cannot figure out how to query the XML, or the attributes of nodes within that XML.
Here is the type of query I want to do:
SELECT
[regular_table_field1],
[regular_table_field2],
[opt_resposne_xml].rootnode.wrapper.child@attr('someattribute') AS 'someattribute',
[opt_resposne_xml].rootnode.wrapper.child@attr('someattribute2') AS 'someattribute2'
FROM [optimizations] WHERE [opt_concept_id] = '1234'
Not knowing what your XML looks like, here’s a general purpose piece of code to give you an idea of what it would look like:
If you need to “reach into” the XML column and grab out a single value (from an XML element or attribute), you basically need to define the XPath to that location of where the information you’re interested in is located, and you need to convert that to a given SQL Server datatype.
If you need to have a single relational row cross joined against a number of elements from a single XML field, you might need other approaches (
CROSS APPLY).Also: you might need to pay attention to XML namespaces that are involved – a common pitfall when trying to parse XML inside SQL Server.