Skip to content
Advertisement

Is using OPENXML() the best way to transform data in an XML Column to Rows and Columns?

I have a SQL Server table with a column of type XML. The XML data represents multiple allowable selections for a given field. An example of the XML data is as follows.

I am using the following code to extract the data from the XML column and transforming it into rows that can be inserted into a new table.

This code is correctly returning the following

Is this the best way of doing this?

What I have here, I think, will need to be in a stored procedure. Ideally I am looking for a way of doing this where I don’t have to worry about losing data because of a buffer overflow due to an unforeseen quantity of data contained in the xml column.

Advertisement

Answer

OPENXML(), and its companions sp_xml_preparedocument/sp_xml_removedocument is a proprietary Microsoft API. It is kept just for backward compatibility with the obsolete SQL Server 2000. SQL Server 2005 onwards supports w3c’s XQuery 1.0, XPath 2.0, and XSD 1.0.

SQL

Output

Advertisement