我有一个用于更新/插入从 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