怎么读取用户自己定义的XML
一般在存储过程里,遇到前台传入的XML可以用类似如下的语句
--@XmlDocument是前台传入的xml 类型是 varchar(max)
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XmlDocument
insert into #XMLTable
SELECT * FROM OPENXML (@DocHandle, '/Roots/Base',2)
WITH (
f1 varchar(20),
f2 uniqueidentifier,
f3 float,
……
……
)
DECLARE @XmlDocumentHandle int
DECLARE @XmlDocument nvarchar(4000)
SET @XmlDocument = N'<ROOT>
<Customer>
<CustomerID>VINET</CustomerID>
<ContactName>Paul Henriot</ContactName>
<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>
<CustomerID>LILAS</CustomerID>
<ContactName>Carlos Gonzlez</ContactName>
<Order OrderID="10283" CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
<OrderDetail ProductID="72" Quantity="3"/>
</Order>
</Customer>
</ROOT>'
EXEC sp_xml_preparedocument @XmlDocumentHandle OUTPUT, @XmlDocument
SELECT *
FROM OPENXML (@XmlDocumentHandle, '/ROOT/Customer',2)
WITH (CustomerID varchar(10),ContactName varchar(20))
SELECT *
FROM OPENXML (@XmlDocumentHandle, '/ROOT/Customer/Order',1)
WITH (OrderID varchar(10),CustomerID varchar(10), EmployeeID int,OrderDate datetime)
EXEC sp_xml_removedocument @XmlDocumentHandle
/*
CustomerIDContactName
VINETPaul Henriot
LILASCarlos Gonzlez
OrderIDCustomerIDEmployeeIDOrderDate
10248VINET51996-07-04 00:00:00.000
10283LILAS31996-08-16 00:00:00.000
*/