場景:購物車提交含多筆明細,欲單次資料庫呼叫完成訂單入庫,減少網路往返。
<ShoppingCart>
<Purchase ProductID="7" Price="10.00" SaleDate="10/11/2006" SaleBatchID="4523" CustomerID="2398"/>
<Purchase ProductID="99" Price="25.00" ... />
...
</ShoppingCart>
DECLARE @idoc INT, @xml VARCHAR(8000)EXEC sp_xml_preparedocument @idoc OUTPUT, @xmlINSERT INTO OrderDetails (ProductID, Price, SaleDate, SaleBatchID, CustomerID)SELECT ProductID, Price, SaleDate, SaleBatchID, CustomerIDFROM OPENXML(@idoc, '/ShoppingCart/Purchase', 2)WITH (ProductID INT, Price MONEY, SaleDate DATETIME, SaleBatchID INT, CustomerID INT)EXEC sp_xml_removedocument @idoc優勢:單次往返、集合操作、無游標、記憶體中解析極快。適用於批次匯入、Web Service 介接、異構同步等場景。