Home » SQL & PL/SQL » SQL & PL/SQL » MERGE: DELETE CLAUSE definition explanation. (Oracle 11g)
MERGE: DELETE CLAUSE definition explanation. [message #644998] Mon, 23 November 2015 06:52 Go to next message
arifs3738
Messages: 39
Registered: November 2015
Location: India
Member

SQL QUERY:

MERGE INTO emp_tgt tgt
USING emp src
ON (tgt.empno=src.empno)
WHEN MATCHED THEN
UPDATE SET tgt.ename = src.ename,
tgt.job=src.job,
tgt.mgr=src.mgr,
tgt.hiredate=src.hiredate,
tgt.sal=src.sal,
tgt.comm=src.comm,
tgt.deptno=src.deptno
DELETE WHERE tgt.sal < 5000
WHEN NOT MATCHED THEN
INSERT (tgt.empno, tgt.ename, tgt.job, tgt.mgr, tgt.hiredate, tgt.sal, tgt.comm, tgt.deptno)
VALUES (src.empno, src.ename, src.job, src.mgr, src.hiredate, src.sal, src.comm, src.deptno);


When using MERGE for the DELETE operation, remember that:
b) DELETE works only on rows updated during MERGE. Any rows in the target table that are not processed during MERGE are not deleted, even if they match the DELETE condition.

Question:
In the above query i was updating two rows where one row had sal more than 5K and one row less than 5K after updating. As per above statement i wrote DELETE clause as well so there should have been only 1 record in tgt table with existing records in tgt table. Am i right?
But this is not happening....at the end only one record is in tgt table???
Please advice???
Re: MERGE: DELETE CLAUSE definition explanation. [message #645000 is a reply to message #644998] Mon, 23 November 2015 07:01 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Quote:
But this is not happening....

POst your test case and your code and your expected results along with your actual results.
Re: MERGE: DELETE CLAUSE definition explanation. [message #645002 is a reply to message #644998] Mon, 23 November 2015 07:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Also format your statement.
If you don't know how to do it, learn it using SQL Formatter.

In the end, feedback in your previous topics, answer the questions, post the solutions you found and thank people for their time to help you.

Re: MERGE: DELETE CLAUSE definition explanation. [message #645004 is a reply to message #644998] Mon, 23 November 2015 08:48 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
arifs3738 wrote on Mon, 23 November 2015 07:52
[b]
DELETE works only on rows updated during MERGE. Any rows in the target table that are not processed during MERGE are not deleted, even if they match the DELETE condition.


Correct. And on updated rows means DELETE condition is applied AFTER update. So it doesn't matter if tgt.sal was < 5000 before MERGE did update. Row will not be deleted if tgt.sal was updated to >= 5000.

SY.

Previous Topic: opposite of listagg
Next Topic: How to get first and last day for 13 month back
Goto Forum:
  


Current Time: Fri Apr 26 13:15:35 CDT 2024