How can I create an Upsert functionality for a table with a composite key when sending the query via php?
I have an order_items table whose key is both an order number and an item number. I then have an api to a store that returns an xml document of order elements (that I then parse into arrays). I want to take this string and update the ones that already exist and insert the ones that don't. Since there could be hundreds of orders (and possibly thousands of line items), I don't want to run a query to check if the item exists each for each line item.
I thought Merge might be the answer, but my data doesn't exist in a table (it is an array that I am going to write to an a table). Is there a way to merge a string of data with an existing table? For example, something equivalent to:
$query = "IF a.order_id and a.item_id --already exist
update db.dbo.order_items
SET a.quantity = $qty, a.status = $status ....
ELSE
INSERT (order_id, item_id, quantity, status)
VALUES
($order_id, $item_id, $qty, $status)";
run_query($query);
Thanks for any help.