MERGE: DELETE CLAUSE definition explanation. [message #644998] |
Mon, 23 November 2015 06:52 |
|
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 #645004 is a reply to message #644998] |
Mon, 23 November 2015 08:48 |
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.
|
|
|