SQL Server 2014 - UPSERT XML Multiple rows We have an input like this  

    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;Orders xmlns:jjsales:api:sales&gt;
        &lt;Order&gt;
            &lt;OrderID&gt;334&lt;/OrderID&gt;
            &lt;AmountPaid currencyID="EUR"&gt;17.10&lt;/AmountPaid&gt;
            &lt;UserID&gt;marc58&lt;/UserID&gt;
            &lt;ShippingAddress&gt;
                &lt;Name&gt;Marc Juppé&lt;/Name&gt;
                &lt;Address&gt;Rue&lt;/Address&gt;
                &lt;City&gt;Paris&lt;/City&gt;
                &lt;StateOrProvince&gt;&lt;/StateOrProvince&gt;
                &lt;Country&gt;FR&lt;/Country&gt;
                &lt;Phone&gt;333333333&lt;/Phone&gt;
                &lt;PostalCode&gt;22222&lt;/PostalCode&gt;
             &lt;/ShippingAddress&gt;
             &lt;ShippingCosts&gt;4.50&lt;/ShippingCosts&gt;
             &lt;Lines&gt;
                &lt;Line&gt;
                   &lt;Details&gt;
                        &lt;ItemID&gt;3664&lt;/ItemID&gt;
                        &lt;Store&gt;47&lt;/Store&gt;
                        &lt;Title&gt;MCPU DDA010&lt;/Title&gt;
                        &lt;SKU&gt;mmx&lt;/SKU&gt;
                    &lt;/Details&gt;
                    &lt;LineID&gt;1111&lt;/LineID&gt;
                    &lt;Quantity&gt;1&lt;/Quantity&gt;
                    &lt;Price currencyID="EUR"&gt;6.2&lt;/Price&gt;
                &lt;/Line&gt;
                &lt;Line&gt;
                   &lt;Details&gt;
                        &lt;ItemID&gt;3665&lt;/ItemID&gt;
                        &lt;Store&gt;45&lt;/Store&gt;
                        &lt;Title&gt;MCPU DFZ42&lt;/Title&gt;
                        &lt;SKU&gt;mmy&lt;/SKU&gt;
                    &lt;/Details&gt;
                    &lt;LineID&gt;1112&lt;/LineID&gt;
                    &lt;Quantity&gt;2&lt;/Quantity&gt;
                    &lt;Price currencyID="EUR"&gt;3.2&lt;/Price&gt;
                &lt;/Line&gt;
            &lt;/Lines&gt;
        &lt;/Order&gt;
    &lt;/Orders&gt;


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())[1]', 'int') as ItemID,
           I.X.value('(Details/Store/text())[1]', 'int') as Store,
           I.X.value('(Details/Title/text())[1]', 'nvarchar(100)') as Title,
           I.X.value('(Details/SKU/text())[1]', 'nvarchar(100)') as SKU,
           I.X.value('(Quantity/text())[1]', 'int') as Quantity,
           I.X.value('(Price/text())[1]', 'decimal(11,2)') as Quantity,
           O.X.value('(OrderID/text())[1]', 'int') as OrderID,
           O.X.value('(AmountPaid/text())[1]', 'decimal(11,2)') as AmountPaid,
           O.X.value('(UserID/text())[1]', 'nvarchar(100)') as UserID,
           O.X.value('(ShippingCosts/text())[1]', '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
Can suggest how to get the value of LineID while parsing the XML?

You can take this output in a variable to check for existence.
DECLARE @input XML = '&lt;Orders&gt;
        &lt;Order&gt;
            &lt;OrderID&gt;334&lt;/OrderID&gt;
            &lt;AmountPaid currencyID=&quot;EUR&quot;&gt;17.10&lt;/AmountPaid&gt;
            &lt;UserID&gt;marc58&lt;/UserID&gt;
            &lt;ShippingAddress&gt;
                &lt;Name&gt;Marc Juppé&lt;/Name&gt;
                &lt;Address&gt;Rue&lt;/Address&gt;
                &lt;City&gt;Paris&lt;/City&gt;
                &lt;StateOrProvince&gt;&lt;/StateOrProvince&gt;
                &lt;Country&gt;FR&lt;/Country&gt;
                &lt;Phone&gt;333333333&lt;/Phone&gt;
                &lt;PostalCode&gt;22222&lt;/PostalCode&gt;
             &lt;/ShippingAddress&gt;
             &lt;ShippingCosts&gt;4.50&lt;/ShippingCosts&gt;
             &lt;Lines&gt;
                &lt;Line&gt;
                   &lt;Details&gt;
                        &lt;ItemID&gt;3664&lt;/ItemID&gt;
                        &lt;Store&gt;47&lt;/Store&gt;
                        &lt;Title&gt;MCPU DDA010&lt;/Title&gt;
                        &lt;SKU&gt;mmx&lt;/SKU&gt;
                    &lt;/Details&gt;
                    &lt;LineID&gt;1111&lt;/LineID&gt;
                    &lt;Quantity&gt;1&lt;/Quantity&gt;
                    &lt;Price currencyID=&quot;EUR&quot;&gt;6.2&lt;/Price&gt;
                &lt;/Line&gt;
                &lt;Line&gt;
                   &lt;Details&gt;
                        &lt;ItemID&gt;3665&lt;/ItemID&gt;
                        &lt;Store&gt;45&lt;/Store&gt;
                        &lt;Title&gt;MCPU DFZ42&lt;/Title&gt;
                        &lt;SKU&gt;mmy&lt;/SKU&gt;
                    &lt;/Details&gt;
                    &lt;LineID&gt;1112&lt;/LineID&gt;
                    &lt;Quantity&gt;2&lt;/Quantity&gt;
                    &lt;Price currencyID=&quot;EUR&quot;&gt;3.2&lt;/Price&gt;
                &lt;/Line&gt;
            &lt;/Lines&gt;
        &lt;/Order&gt;
    &lt;/Orders&gt;
'


   SELECT
      LineId = XCol.value('(LineID)[1]','int')
      FROM 
      @input.nodes('/Orders/Order/Lines/Line') AS XTbl(XCol)

Here is the result set I get:

LineId
1111
1112