Insert new rows and update existing ones in one logical operation.
Section: INSERT, UPDATE, and DELETE
Merge or upsert data
sql
sql
MERGE INTO inventory AS target
USING incoming_inventory AS src
ON target.sku = src.sku
WHEN MATCHED THEN UPDATE SET quantity = src.quantity
WHEN NOT MATCHED THEN INSERT (sku, quantity) VALUES (src.sku, src.quantity);Explanation
MERGE support and exact syntax vary by dialect. Some engines use ON CONFLICT or ON DUPLICATE KEY UPDATE instead.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing INSERT, UPDATE, and DELETE