My data looks like below:
<products>
<product ProductID="1" Price="79.99" Weight="30.00" Quantity="1">
<addon ProductAddonID="0" ControlTypeID="9" Price="25.00" Weight="0.00" Quantity="1" Name="yyy" Data="ASD" />
<addon ProductAddonID="89" ControlTypeID="0" Price="15.00" Weight="4.00" Quantity="1" Name="xxx" Data="" />
</product>
</products>
My SQL code looks like this:
INSERT INTO [Order].Items(OrderID, ProductID, Price, Weight, Quantity)
SELECT @OrderID, ProductID, Price, Weight, Quantity
FROM OPENXML (@XmlHandle, '/products/product',1)
WITH (ProductID INT '@ProductID',
Price DECIMAL(6,2) '@Price',
Weight DECIMAL(6,2) '@Weight',
Quantity INT '@Quantity')
SET @OrderItemId = SCOPE_IDENTITY()
INSERT INTO [Order].Addons(OrderItemID, ProductAddonID, ControlTypeID, Price, Weight, Quantity, [Name], DATA)
SELECT @OrderItemId, ProductAddonID, ControlTypeID, Price, Weight, Quantity, [Name], [Data]
FROM OPENXML(@XMLHandle, '/products/product/addon',1)
WITH (
ProductAddonID INT,
ControlTypeID INT,
Price DECIMAL(6,2),
Weight DECIMAL(6,2),
Quantity INT,
[Name] NVARCHAR(500),
[Data] NVARCHAR(max)
)
When I have multiple products/addons, all of the addons are inserting with the latest @OrderItemID ... I'm not sure how to work in my SQL that inserts the addon into the loop that iterates through the product nodes.
Could anyone point me in the right direction?
thanks in advance!