Skip to main content

Oracle Merge Statement Delta Detection


The MERGE Syntax by Ajay Nerumati


Delta Detection in Oracle SQL

Delta detection is a common task in every Data Warehouse. It compares new data from a source system with the last versions in the Data Warehouse to find out whether a new version has to be created. There are several ways to implement this in Oracle.

Your source system delivers a full extraction every night, and you have to load only the changed rows into your Core Data Warehouse? You receive incremental loads from another source system every few minutes, but only a few columns are loaded into the Data Warehouse.

In all these situations, you need a delta detection mechanism to identify the rows that have to be inserted or updated in your Data Warehouse tables. In this blog post, I want to show different methods in Oracle SQL that provide the subset of rows of a source table that were changed since the last load. All these methods are set-based, i.e. they can be executed in one SQL statement.


Merge is used to conditionally insert or update a table .

To understand the merge statement we will use two tables DEPT and DEPT_COPY. The objective being that we load data from DEPT table conditionally into DEPT_COPY .

Step 1  Preparing DEPT and DETP_COPY tables

Based on a much familiar table DEPT that has 4 records, a new table DEPT_COPY with no records  is created using the following syntax .
create table dept_copy as select * from dept where 1=2 ;
Check if the table "DEPT_COPY" is empty and without any records using the following syntax.
Select * from DEPT_COPY;

Step 2  Understanding the Merge Syntax before implementation

INTO  clause specifies the target table you are updating or inserting into.
USING clause identifies the source of the data to be updated or inserted; can be a table, view, or subquery
ON clause the condition upon which the MERGE operation either updates or inserts
WHEN MATCHED | WHEN NOT MATCHED instructs the server how to respond to the results of the join condition
  
Step 3 Implementing the Merge Syntax

Execute the following syntax:
MERGE INTO dept_copy c
USING dept d
ON (c.deptno = d.deptno)
WHEN MATCHED THEN
UPDATE SET c.dname = d.dname, c.loc = d.loc
WHEN NOT MATCHED THEN
INSERT VALUES(d.deptno, d.dname, d.loc);

After executing the above command all the data from table "DEPT" is loaded into "DEPT_COPY".
Query the table DEPT_COPY to confirm that it is populated with all the 4 rows from DEPT.

Step 4 Checking for UPDATES on DEPT to reflect in DEPT_COPY

To test the effect of  MERGE command , make modifications to the source table "DEPT" as follows :-
update dept set loc ='HYDERABAD' where deptno =10;
commit;
Re-run the same MERGE command without making any changes.

MERGE INTO dept_copy c
USING dept d
ON (c.deptno = d.deptno)
WHEN MATCHED THEN
UPDATE SET c.dname = d.dname, c.loc = d.loc
WHEN NOT MATCHED THEN
INSERT VALUES(d.deptno,d.dname,d.loc);

Query the target table "DEPT_COPY" to check if the changes for the loc column in the source "DEPT" are reflecting in the target table.
Select * from DEPT_COPY.
you should find all the updates done on source reflecting in the target.

Step 5 Checking for INSERT on DEPT to reflect in DEPT_COPY

Insert the following row into DEPT table and run the merge command.
insert into dept values ( 50, 'Software', 'Mumbai');
commit;
check the insert table in the source by querying
select * from dept;
Re-run the MERGE command without making any changes to check if the record is loaded into DEPT_COPY.

MERGE INTO dept_copy c
USING dept d
ON (c.deptno = d.deptno)
WHEN MATCHED THEN
UPDATE SET c.dname = d.dname, c.loc = d.loc
WHEN NOT MATCHED THEN
INSERT VALUES(d.deptno,d.dname,d.loc);

Query the target table "DEPT_COPY" to check if the changes in the source "DEPT" are reflecting in the target table.
Select * from DEPT_COPY.
You should find any inserts done on source reflecting in the target.

Merge statement,  can update or insert a row conditionally into a table, thus avoiding multiple UPDATE statements. The decision whether to update or insert into the target table is based on a condition in the ON clause.














Comments

pooja said…
it was really helpful.. we can avoid writing multiple update statements :)
mahanth said…
explaining the topic not only in training session but also providing the reference in the blog makes to learn more.
ajay said…
very helpful content
bhagyesh said…
This comment has been removed by the author.
Shiladitya said…
very helpful and easily understandable :)
sumanta said…
the explanation in this blog about merge is really helpful.
VAISHNAVI said…
it was really helpful.Thankyou sir for your guiding.
Shivam said…
Easy to understand
Unknown said…
It is very helpful understanding merge operation in a easy way.All doubts cleared.Thank you very much Sir
Rehan Ahmed said…
This is a very helpful tutorial on merge. All the steps are very clearly written with explaination.
Mahesh Jagtap said…
very useful blog ,complete understanding of MERGE keyword ,Thanks a lot.
bhagyesh said…
very helpful and easy to learn
preeti said…
it was really helpful to learn ....
preeti said…
it was really helpful to learn ....
Akash Sinha said…
Thanks a lot for explaining most important topic of SQL in a very simple manner.
Unknown said…
The topic is well explained and very helpful.
Aditya said…
the concept is thoroughly explained here and it is very easy to understand.
Yash said…
It is a very helpful blog, one can easily learn 'MERGE'from this blog.
Shubham said…
Understanding merge came out to be very easy and hepful from the blog.
sandeep said…
it is very helpful and easy to understand the entire concept
Aditya said…
This comment has been removed by the author.
Unknown said…
Helpful and easily accessable. For future references.
Chandrika said…
Each and every step is explained so clearly such that there is no ambiguity in doing queries
You are an awesome trainer Ajay Sir .
You made student to think about it by u r way of teaching and coming to this Merge statement it is very much useful to me Thanks for u r content and this is clear that way u have explained in Steps .
Anudeep said…
Very helpful blog and easy to understand...
vinay said…
Providing reference in the blog while explaining in the training session is helping me alot. Thank you.
Vidya latha said…
It was really helpful...
Medini said…
Sir,It was really helpful and understanding enough to execute query using the merge command.Thank you so much for the explanation in the blog.
Gayathri said…
Easy and understandable
Gayathri said…
Easy and understandable
Gayathri said…
Easy and understandable
Chandrika said…
Each and every step is explained so clearly such that there is no ambiguity in doing queries
vinay said…
Providing reference in the blog while explaining in the training session is helping me alot. Thank you.
Unknown said…
It is easy to understand and thank u sir for providing clear information about the merge statements
vinay said…
Vinay said....
Providing reference in the blog while explaining in the training session is helping me alot. Thank you.
Unknown said…
It was really helpful to execute query using the merge command.
Vidya latha said…
It is really helpful and understandable
Gayathri said…
Easy and understandable
Unknown said…
Nice sir
Awesome information
Unknown said…
Each and every statement is explained clearly and this blog is very helpfull to understand merge.
Pavan kumar said…
Fantastic explanation
Krishna said…
The topic is well explained and very helpful.
Unknown said…
The content was helpful to understand thouroughly.
Unknown said…
Content was taught was easily understandable with very good concept.
Krishna said…
The topic is well explained and very helpful.
Krishna said…
The topic is well explained and very helpful.
Unknown said…
Content was helpful and understandable.

By priya
Unknown said…
Topics well explained and understandable
Thank you sir
By Vandana
Rahul said…
Very helpful to understand merge
Unknown said…
Concepts are very clear in short period of time.
The topic is well explained and very helpful
Ramya said…
Simple and easy to understand. Thank you sir!
Anuradha H D said…
Thank you for the useful content. It is clear and understandable.
Nisha said…
Easy to understand.
Arghadeep said…
Nice session it was and also very helpful.
Unknown said…
Topics are well explained easy to understand.Thank you sir!
Unknown said…
Thank you sir ,really helpful
Krishna said…
Merge topic was clearly explained and very easy to understand
Unknown said…
Easy to understand
Rakesh said…
Useful and clearly understood.
Unknown said…
It is good for understanding in short time.
Pooja said…
It was easy and the language used was understandable.
Unknown said…
Very helpful and easily understandable
Puja said…
It's easy to understand the and also helpful
RaghuTammana said…
This comment has been removed by the author.
Praveen said…
It is easy to understand and well explained.
Shivangi Singh said…
Easy to understand and helpful
Unknown said…
Easily understood and topics are well explained
Unknown said…
This is really helpful.
Unknown said…
By Bharathi
It was very helpful and it is easy to understand.
Unknown said…
It is very easy to understand..
Tejas Ingole
Shivangi Singh said…
Easy to learn and understandable
Rajvi said…
This comment has been removed by the author.
Unknown said…
Very useful blog ,complete understanding of MERGE keyword ,Thanks a lot.

By Leela Krishna boddapati

RaghuTammana said…
This comment has been removed by the author.
Unknown said…
Very helpful
Saurabh Dubey
Unknown said…
Pallavi
It is easy to understand and useful
Praveen said…
It was well explained and can be understandable easily.
By praveen
Harshada Mamde said…
Very helpful and easy to understand.
Harshada Mamde
Jigeesha said…
Well explained!
Mohit Sandhuria said…
Very helpful and easy to understand.
RaghuTammana said…
Understandable🙂
Ayush Gaur said…
Good elaborative explaination.
Gaurang Gautami said…
Easily Understandable and Explained well,
Thank you!!
Madhuri said…
Thank you sir, it's really helpfull & and easy to understand
Unknown said…
You explained the merge command clearly .It's very useful to understand.
Unknown said…
Very helpful and useful.
Gautam Buragohain
Unknown said…
Very helpful and easy to understand
By Faizan Inamdar
Harshada Mamde said…
Very helpful and easy to understand.
Rajvi said…
Easy to understand and useful.
Unknown said…
Very helpful sir
Mohit singh said…
Very helpful and easy to understand.
-Mohit
Unknown said…
Very helpful and easy to understand
Prasad said…
Its very helpful and easy to understand
Unknown said…
Helpful nd useful
Sumanta said…
Welll set of example,nice explanation,thank you
Unknown said…
It was helpful for merge command.Explanation is good.
Very helpful, Well explained with
Easy and Understandable manner! ✌️
shruthi said…
It is very useful
My Profile said…
Good explanation with good example.Thanks for support us
Unkwn said…
Good explanation with good example.Thanks for support us

May 2, 2019 at 10:09 PM
Karthick Elan said…
Easy to understand about the concept of merge and the explaination of syntax was great. Thanks!
Aatish Raj said…
This comment has been removed by the author.
Gaurang said…
Detailed explanation of merge statement.
Divyanshu said…
Easy to understand and very helpful.
Thank you ajay.
Aatish Raj said…
Good explanation in a very simple and understandable manner.It is very helpful. Thank you
Unknown said…
It was very helpful
Unknown said…
Thank you sir. It is easier to understand
Unknown said…
This was very helpful
It makes easy to understand merge
Command. Its very helpful.
Unknown said…
This was really helpful and made easy to understand merge command
MK said…
It was very helpful and understandable.
MK said…
It was very helpful and understandable.
AMISHA GUPTA said…
This comment has been removed by the author.
AMISHA GUPTA said…
Detailed and clear explanation of merge command
This is a very clear and detailed explanation. Indeed helpful!
The content was very helpful. It gave a clear understanding of the merge command.
Rinitha Jose said…
The clear and detailed explanation makes understanding the concept a lot easier.
Samreen khan said…
It was very helpful and easy to understand!
This comment has been removed by the author.
Gowtham reddy said…
This comment has been removed by the author.
Muthukumar K said…
i got cleared abt this topic and it is simple the way u explained.
thankyou
Shashi said…
i was very hepful,got evrything about merge command
Gowtham reddy said…
The clear and detailed explanation makes understanding the concept a lot easier and helpfull.
Phani krishna said…
Very detail and helpful
Unknown said…
so basically, MERGE statement is used to synchronize two tables by inserting, deleting, and updating the target table rows based on the join condition with the source table.
Unknown said…
very easy explanation
Detail and helpful for understanding the concept
ram said…
It was very helpful and easy to understand.
Jonas Robin said…
This comment has been removed by the author.
Jonas Robin said…
Easy to understand and very helpful
Jonas Robin said…
Easy to understand and very helpful
Unknown said…
Easy to understand and very helpful
Unknown said…
Easy to understand and very helpful
manikanta said…
This comment has been removed by the author.
manikanta said…
Good explanation with examples. It will be more helpful.
Unknown said…
Easy to understand and well explained
Unknown said…
It was detailed explanation.
Thank you :)
Abhishek said…
It was really helpful and quite easy to understand.
Thank you for this amazing content sir! Much needed.
it's really good better than the official documentation!!!.
keep sharing the knowledge...
Akshay patil said…
excellent content....nicely explained.
rishav said…
topic is very well covered with very good explanations step wise... Thank You for this
saptarshi said…
merge is one of the important topics and its well explained
It's very good and simple to understand the merge concept for somebody new to the concept.thanks for helping us understand.
G Karibasava said…
Content is soo good . we can easily understand.
G Karibasava said…
we can easily understand content. Nice explained.
AJAY said…
Thanks for the simple and detailed explanation.....Its easy to learn
G Karibasava said…
simple and detailed explanation.....Its easy to learn
nikhil said…
Explained in a simple and understanding manner. Thank you.
SURAJ S said…
A good tutorial on merge. All the steps are very clearly stated with explanations.
Shiva Surabhi said…
Very clearly explained about merge thankyou
Pranav said…
It was easy to understand concept the step by step procedure helps a lot in understanding it in a better way
amir said…
Simple steps. Easy to understand.
Unknown said…
Easy to understand..
it's really good better than the official documentation.
keep sharing the knowledge.
Unknown said…
it was really helpful.
Useful and clearly understood.
Joy said…
thank you for clearing the concepts
Unknown said…
easy to understand
Unknown said…
really, it was easy to understand
Unknown said…
Easy to understand
Arunangshu Sen said…
This comment has been removed by the author.
Arunangshu Sen said…
Clear explanations for complicated topics.
Sowmya said…
Great content! Please keep going..! It will realy help many of us.
D said…
Helpful content
Manish said…
Detailed information for beginners.Thank you!!!
Tejus said…
Very helpful and easy to understand the concept.
Guruprasad p said…
it was really helpul ,thank you
ANKIT ANAND said…
very easy to understand
Sadananda said…
Great content, easily understandable
Sourav Nandy said…
simple and easy explanation...
Hari Charan said…
Well!Tough topic with simple words and sentences.Just amazing work keep sharing!
Karthik gowda k said…
IT WAS REALLY HELPFUL, EASY TO UNDERSTAND
SURAJ S said…
Clearly explaned complicated topics. Good work.
~ Suraj
Pavankumar.e said…
This comment has been removed by the author.
Pavankumar.e said…
This is very helpful and we can use it in rush timings to learn in stipulated times.

- Pavankumar.e
Unknown said…
The topic is well explained and very helpful.
Prateek Kumar said…
Step by step explanation really helped to understand the topic. Very helpful..
Shrikant Singh said…
Very clearly explained about merge thankyou
nikhil said…
Simple to understand and extremely useful.
jathin said…
The concept is thoroughly explained here and it is very easy to understand.

Popular posts from this blog

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C   NUMBER(3); BEGIN WHILE B <

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7     1.      Introduction This document outlines a mini project for the RDBMS LOT

sample tables

  --sample tables DROP TABLE EMP; DROP TABLE DEPT; DROP TABLE BONUS; DROP TABLE SALGRADE; DROP TABLE DUMMY;   CREATE TABLE EMP        (EMPNO NUMBER(4) NOT NULL,         ENAME VARCHAR2(10),         JOB VARCHAR2(9),         MGR NUMBER(4),         HIREDATE DATE,         SAL NUMBER(7, 2),         COMM NUMBER(7, 2),         DEPTNO NUMBER(2));   INSERT INTO EMP VALUES         (7369, 'SMITH',   'CLERK',      7902,         TO_DATE('17-DEC-1980', 'DD-MON-YYYY'),   800, NULL, 20); INSERT INTO EMP VALUES         (7499, 'ALLEN',   'SALESMAN',   7698,         TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600,   300, 30); INSERT INTO EMP VALUES         (7521, 'WARD',    'SALESMAN',   7698,         TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250,   500, 30); INSERT INTO EMP VALUES         (7566, 'JONES',   'MANAGER',    7839,         TO_DATE('2-APR-1981', 'DD-MON-YYYY'),   2975, NULL,