Skip to Main Content

Write Great SQL Office Hours

Free tips and training every month! Subscribe for reminders and more from Office Hours. FAQ

Header container

July 19, 2022

13:00 UTC   Start Times Around the World

Subscribe to be notified of changes to sessions and give us feedback!

Having trouble watching the video on this page? Open the video in your browser.


How to UPSERT (INSERT or UPDATE) rows with MERGE in Oracle Database
Often when loading data you want to add new rows and change the existing ones. So you need "update if exists, insert if not exists" logic, aka UPSERT. This can lead to complex statements and conditional logic.

The MERGE statement enables you to do both actions in one SQL statement.

This session covered the details of how this works. Highlights include:

05:15 - Overview of MERGE syntax
10:10 - Merging bind variable values
12:30 - Demo of MERGE
21:40 - Getting the INSERT vs UPDATE split for merged rows
22:30 - Using triggers to record INSERT vs UPDATE
25:00 - INSERT vs UPDATE count Flashback versions query
27:10 - INSERT vs UPDATE count with SQL Monitoring
33:35 - ORA-30926 unable to get a stable set of rows error
41:10 - Deleting rows in the target missing from the source
43:20 - Demo of insert vs update count, ORA-30926 error and deleting missing
52:30 - Performance of MERGE
57:50 - Demo of update only MERGE performance compared to UPDATEs
1:03:40 - Recap

Get the scripts for this session
Improve Your SQL Skills with this FREE course html

Your Experts

    Chris Saxon

    Chris Saxon   

    Chris Saxon is an Oracle Developer Advocate and quizmaster on Dev Gym. His job is to help you get the best out of the Oracle Database and have fun with SQL! He's also part of the answer team on Ask TOM and creates YouTube videos at The Magic of SQL channel.