I have a table SALARY_MASTER which contains empid
and salary
. Below is the table structure.
EMPID SALARY
------- ------
10001 12000
10002 60000
10003 25000
10004 35000
10005 15000
10006 24000
10007 85000
10008 75000
10009 65000
10010 67000
10011 95000
10012 23000
10013 48000
10014 25000
10015 35000
Another table named SALARY_CURRENT
with same columns. Below is the sample data.
EMPID SALARY
------- ------
10001 24000
10003 36000
10005 23000
10007 99000
10009 79000
10016 52000
10017 98000
10018 63000
10019 77000
10020 47000
10021 35000
This table contains updated salary for employees. In every quarter I get this table from higher authority and I need to update the SALARY_MASTER
table based on the SALARY_CURRENT
table. So I used merge
command in this case. Below is my merge
statement:
MERGE into SALARY_MASTER SM
USING
SALARY_CURRENT SC
ON
(SM.EMPID = SC.EMPID)
WHEN MATCHED THEN
UPDATE SET SM.SALARY = SC.SALARY
WHEN NOT MATCHED THEN
INSERT (SM.EMPID,SM.SALARY) VALUES(SC.EMPID,SC.SALARY);
After merging my SALARY_MASTER
, the table is like below:
EMPID SALARY
------- ------
10001 24000
10002 60000
10003 36000
10004 35000
10005 23000
10006 24000
10007 99000
10008 75000
10009 79000
10010 67000
10011 95000
10012 23000
10013 48000
10014 25000
10015 35000
10016 52000
10017 98000
10018 63000
10019 77000
10020 47000
10021 35000
I want only affected rows. My output table will be like below:
EMPID SALARY STATUS
------- ------- ------
10001 24000 UPDATE
10002 60000 NONE
10003 36000 UPDATE
10004 35000 NONE
10005 23000 UPDATE
10006 24000 NONE
10007 99000 UPDATE
10008 75000 NONE
10009 79000 UPDATE
10010 67000 NONE
10011 95000 NONE
10012 23000 NONE
10013 48000 NONE
10014 25000 NONE
10015 35000 NONE
10016 52000 INSERT
10017 98000 INSERT
10018 63000 INSERT
10019 77000 INSERT
10020 47000 INSERT
10021 35000 INSERT
Or:
EMPID SALARY STATUS
------- ------- ------
10001 24000 UPDATE
10003 36000 UPDATE
10005 23000 UPDATE
10007 99000 UPDATE
10009 79000 UPDATE
10016 52000 INSERT
10017 98000 INSERT
10018 63000 INSERT
10019 77000 INSERT
10020 47000 INSERT
10021 35000 INSERT
I am using Oracle 11g. Actual table contains more than 300k values.
Use the EXIST/NOT EXISTS with UNION will solve your problem, check out the below code: