if new row is inserted into table, if duplicate rows already present i want to merge the two rows into one
table TEST ( ID column defined as primary key)
ID ITEM QUANTITY -- ---- -------- 1 KA1 5 2 KA2 2
if new row is inserted to table test with values (KA1,6), because item KA1 already present new row should be inserted with total quantity 11 and old row should be deleted .
Result set should be as the following :
ID ITEM QUANTITY -- ---- -------- 2 KA2 2 3 KA1 11
where used trigger and inline procedure are :
CREATE OR REPLACE TRIGGER MERG_DUP
BEFORE INSERT ON TEST
FOR EACH ROW
BEGIN
FOR VAL IN(SELECT ID,ITEM,QUANTITY, FROM TEST)
LOOP
IF VAL.ITEM=:NEW.ITEM THEN
:NEW.QUANTITY:=:NEW.QUANTITY+VAL.QUANTITY;
XXI_MULTI_PR_REMOVE(VAL.ID);
EXIT;
END IF;
END LOOP;
end;
/
CREATE OR REPLACE PROCEDURE XXI_MULTI_PR_REMOVE(ID number)
IS
PRAGMA AUTONOMOUS_TRANSACTION;
L_TID NUMBER;
BEGIN
L_TID:=ID;
DELETE FROM TEST WHERE ID=L_TID;
commit;
END;
/
quantitywith amerge into. - Kaushik Nayak{}symbol on the toolbar to change the display format. - Kaushik Nayak