XML から読み取ったデータを更新/挿入するためのストアド プロシージャがあります。itemResults の下の xml データでわかるように、複数 (この場合は 2 つ) の項目があります。値が変更されたアイテムのみを更新する必要があります。私のストアド プロシージャでは、デフォルトで最初の項目の値を取得します。アイテムの値は明らかに実行時に変更されます。同様に、いいえ。のアイテムがユーザーによって追加された場合 (新しいアイテムが追加された場合)、データベースに保存する必要があります。
XML データ:
<?xml version="1.0" encoding="utf-16"?>
<SaveOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OrderID>10511</OrderID>
<RowID>0</RowID>
<EmployeeID>0</EmployeeID>
<OrderDate>4/18/1997</OrderDate>
<ShipVia>0</ShipVia>
<Freight>0</Freight>
<ContactName>Laurence Lebihan</ContactName>
<Phone>91.24.45.40</Phone>
<ItemCount>0</ItemCount>
<orderResults>
<Orders>
<OrderID>10511</OrderID>
<RowID>68</RowID>
<CustomerID>BONAP</CustomerID>
<EmployeeID>4</EmployeeID>
<OrderDate>4/18/1997</OrderDate>
<RequiredDate>5/16/1997</RequiredDate>
<ShippedDate>5/16/1997</ShippedDate>
<ShipVia>3</ShipVia>
<Freight>420</Freight>
<ShipName>Bon app'</ShipName>
<ShipAddress>12, rue des Bouchers</ShipAddress>
<ShipCity>Marseille</ShipCity>
<ShipRegion />
<ShipPostalCode>13008</ShipPostalCode>
<ShipCountry>France</ShipCountry>
<CompanyName>Bon app'</CompanyName>
<ContactName>Laurence Lebihan</ContactName>
<Phone>91.24.45.40</Phone>
<ItemCount>3</ItemCount>
<itemResults>
<Items>
<OrderID>10511</OrderID>
<ProductID>4</ProductID>
<UnitPrice>22.0000</UnitPrice>
<Quantity>50</Quantity>
<Discount>0.15</Discount>
</Items>
<Items>
<OrderID>10511</OrderID>
<ProductID>7</ProductID>
<UnitPrice>30.0000</UnitPrice>
<Quantity>50</Quantity>
<Discount>0.15</Discount>
</Items>
<Items>
<OrderID>10511</OrderID>
<ProductID>8</ProductID>
<UnitPrice>40.0000</UnitPrice>
<Quantity>10</Quantity>
<Discount>0.15</Discount>
</Items>
</itemResults>
</Orders>
</orderResults>
</SaveOrder>
ストアド プロシージャ: ALTER PROCEDURE [dbo].[usp_UpdateOrderDetail]
@Request XML = '<Request/>'--,
--@Response XML = '<Response/>' OUTPUT
As
BEGIN
DECLARE @OrderID int
DECLARE @ProductID nvarchar(50)
SET @OrderID = @Request.value('(SaveOrder/OrderID)[1]', 'int')
IF @OrderID > 0
BEGIN
UPDATE Customers SET
ContactName = T.C.value('(ContactName/text())[1]', 'nvarchar(30)'),
Phone = T.C.value('(Phone/text())[1]', 'nvarchar(24)')
--OrderDate = T.C.value('OrderDate[1]', 'varchar(50)')
FROM @Request.nodes('/SaveOrder/orderResults/Orders') as T(C)
WHERE CustomerID = T.C.value('(CustomerID/text())[1]', 'nchar(5)')
UPDATE Orders SET
OrderDate = T.C.value('(OrderDate/text())[1]', 'datetime'),
RequiredDate = T.C.value('(RequiredDate/text())[1]', 'datetime'),
ShippedDate = T.C.value('(ShippedDate/text())[1]', 'datetime'),
ShipVia = T.C.value('(ShipVia/text())[1]', 'int'),
Freight = T.C.value('(Freight/text())[1]', 'money'),
ShipName = T.C.value('(ShipName/text())[1]', 'nvarchar(40)'),
ShipAddress = T.C.value('(ShipAddress/text())[1]', 'nvarchar(60)'),
ShipCity = T.C.value('(ShipCity/text())[1]', 'nvarchar(15)'),
ShipRegion = T.C.value('(ShipRegion/text())[1]', 'nvarchar(15)'),
ShipPostalCode = T.C.value('(ShipPostalCode/text())[1]', 'nvarchar(10)'),
ShipCountry = T.C.value('(ShipCountry/text())[1]', 'nvarchar(15)')
FROM @Request.nodes('/SaveOrder/orderResults/Orders') as T(C)
WHERE OrderID = @OrderID
UPDATE [Order Details] SET
UnitPrice = T.C.value('(UnitPrice/text())[1]', 'money'),
Quantity = T.C.value('(Quantity/text())[1]', 'smallint'),
Discount = T.C.value('(Discount/text())[1]', 'real')
FROM @Request.nodes('/SaveOrder/orderResults/Orders/itemResults/Items') as T(C)
WHERE OrderID = @OrderID
END
ELSE
BEGIN
INSERT INTO Customers
(ContactName,
Phone)
SELECT T.C.value('(ContactName/text())[1]', 'nvarchar(30)'),
T.C.value('(Phone/text())[1]', 'nvarchar(24)')
FROM @Request.nodes('/SaveOrder/orderResults/Orders') as T(C)
INSERT INTO Orders(
OrderDate,
RequiredDate,
ShippedDate ,
ShipVia ,
Freight ,
ShipName ,
ShipAddress ,
ShipCity ,
ShipRegion ,
ShipPostalCode ,
ShipCountry )
SELECT T.C.value('(OrderDate/text())[1]', 'datetime'),
T.C.value('(RequiredDate/text())[1]', 'datetime'),
T.C.value('(ShippedDate/text())[1]', 'datetime'),
T.C.value('(ShipVia/text())[1]', 'int'),
T.C.value('(Freight/text())[1]', 'money'),
T.C.value('(ShipName/text())[1]', 'nvarchar(40)'),
T.C.value('(ShipAddress/text())[1]', 'nvarchar(60)'),
T.C.value('(ShipCity/text())[1]', 'nvarchar(15)'),
T.C.value('(ShipRegion/text())[1]', 'nvarchar(15)'),
T.C.value('(ShipPostalCode/text())[1]', 'nvarchar(10)'),
T.C.value('(ShipCountry/text())[1]', 'nvarchar(15)')
FROM @Request.nodes('/SaveOrder/orderResults/Orders') as T(C)
INSERT INTO [Order Details]
(UnitPrice ,
Quantity,
Discount)
SELECT T.C.value('(UnitPrice/text())[1]', 'money'),
T.C.value('(Quantity/text())[1]', 'smallint'),
T.C.value('(Discount/text())[1]', 'real')
FROM @Request.nodes('/SaveOrder/orderResults/Orders/itemResults/Items') as T(C)
END
DECLARE @Counter INT
SET @Counter = 1