Skip to content
Advertisement

Unable to Query XML Document with SQL/OPENXML

I would like to query the following XML-File using SQL:

Using this code I would like to get the value for “mRID”:

However, the result is:

mRID
NULL

How to get the correct value for mRID (‘2f6f8b82348440b1b121bca06311945d’) instead of NULL?

Advertisement

Answer

Microsoft proprietary OPENXML and its companions sp_xml_preparedocument and sp_xml_removedocument are kept just for backward compatibility with the obsolete SQL Server 2000. Their use is diminished just to very few fringe cases.

Starting from SQL Server 2005 onwards, it is better to use XQuery language, based on the w3c standards, while dealing with the XML data type.

Your XML has a default namespace, so it should be taken into account.

SQL, from a variable

SQL, directly from the XML file

Output

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement