I’m getting an error when executing this in SQL Server 2005, But works if I remove the namespaces from the root node :
DECLARE @XmlDocumentHandle int
DECLARE @XmlDocument nvarchar(1000)
SET @XmlDocument = N'<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema>
<Customer>
<ID>VINET</ID>
<Name>Paul Henriot</Name>
<Order OrderID="10248" CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
<OrderDetail ProductID="11" Quantity="12"/>
<OrderDetail ProductID="42" Quantity="10"/>
</Order>
</Customer>
<Customer>
<ID>LILAS</ID>
<Name>Carlos Gonzlez</Name>
<Order OrderID="10283" CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
<OrderDetail ProductID="72" Quantity="3"/>
</Order>
</Customer>
</ROOT>'
-- Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @XmlDocumentHandle OUTPUT, @XmlDocument
-- Execute a SELECT statement using OPENXML rowset provider.
SELECT *
FROM OPENXML (@XmlDocumentHandle, '/ROOT/Customer',2)
WITH (CustomerID varchar(10) 'ID',
ContactName varchar(20) 'Name')
EXEC sp_xml_removedocument @XmlDocumentHandle
You are just missing a double quote at the end of the first line of xml:
(the 2nd-to-last char)
I am able to run your query ok on SQL 2008 after making this change.
John