2

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'
0

1 Answer 1

2

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:

 SELECT 
    [regular_table_field1],
    [regular_table_field2],
    [opt_resposne_xml].value('(/rootnode/wrapper/child/@someattribute)[1]', 'int') AS 'someattribute',
    [opt_resposne_xml].value('(/rootnode/wrapper/child/@someattribute2)[1]', 'varchar(50)') AS 'someattribute2'
 FROM 
     [optimizations] 
 WHERE 
     [opt_concept_id] = '1234'

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so VERY much. I found the MSDN articles that explained how the process worked but could not get the syntax for the queries to work properly. Can you please explain why you had to put the XPath query in (parens) and why there's a [1] at the end of it? Is this the way of specifying the first, second third, etc appearance of that attribute in a set of nodes?
Why parens? I don't know - you'd have to ask the Microsoft designers who defined this. The [1] denotes the first occurence - if you want to convert it to a single data value, you need to make sure it's just a single XML node / attribute being returned. And yes - you could also use [2] to get the second - but be careful to be sure there is a second one....
I was actually trying this out to see how it would work... if I leave off the [1] I get an error... because there is no [2] it returns NULL if I use that. I was basically writing this exact same query but without the extra parens and the bracketed index indicator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.