Sample Header Ad - 728x90

SQL Server 2014 - UPSERT XML Multiple rows

1 vote
1 answer
217 views
We have an input like this 334 17.10 marc58 Marc Juppé Rue Paris FR 333333333 22222 4.50 3664 47 MCPU DDA010 mmx 1111 1 6.2 3665 45 MCPU DFZ42 mmy 1112 2 3.2 and we are currently importing data into Sales Table with a query like this: INSERT INTO Sales (ItemID,Store,Title,SKU,Quantity,Price,OrderID,AmountPaid,UserID,ShippingCosts) SELECT I.X.value('(Details/ItemID/text())', 'int') as ItemID, I.X.value('(Details/Store/text())', 'int') as Store, I.X.value('(Details/Title/text())', 'nvarchar(100)') as Title, I.X.value('(Details/SKU/text())', 'nvarchar(100)') as SKU, I.X.value('(Quantity/text())', 'int') as Quantity, I.X.value('(Price/text())', 'decimal(11,2)') as Quantity, O.X.value('(OrderID/text())', 'int') as OrderID, O.X.value('(AmountPaid/text())', 'decimal(11,2)') as AmountPaid, O.X.value('(UserID/text())', 'nvarchar(100)') as UserID, O.X.value('(ShippingCosts/text())', 'decimal(11,2)') as ShippingCosts FROM @XML.nodes('/Orders/Order') as O(X) CROSS APPLY O.X.nodes('Lines/Line') as I(X) but now we need to update records that already exists checking Lines.Line.LineID Since do not want to use MERGE, we thought it was easy to solve adding the usual IF NOT EXISTS (SELECT 1 FROM Sales where LineID=@LineID) INSERT INTO Sales () SELECT .... ELSE UPDATE Sales SET ItemID=..., Store=..., ... WHERE LineID=@LineID but the value of @LineID in inside the XML and has to be calculated while parsing the XML And discovered have no idea how to get it Can suggest how to get the value of LineID while parsing the XML? Thanks
Asked by Joe (369 rep)
Oct 19, 2017, 06:53 PM
Last activity: Jun 24, 2025, 02:04 AM