This is expected behaviour for MERGE.
See Metalink Note: 554656.1 "Merge Increments SEQUENCE.NEXTVAL for Both Insert and Update"
Question
I'm using Oracle Merge to do an UPSERT. It looked to be working fine until i noticed my SEQ is always incremented by 1 even when the when matched then side of the equation is satisfied. I know that the update side of the equation is being called because the status is updating as required (the name is a primary key so this wouldn't work purely by re-inserting the row). I've included my procedure below. Any ideas why this is happening?
CREATE OR REPLACE PROCEDURE ff_update(argname VARCHAR,
argstate VARCHAR)
AS
BEGIN
MERGE INTO flags f
USING (SELECT argname name FROM dual) b ON (f.name = b.name)
WHEN MATCHED THEN
UPDATE SET f.state = argstate
WHEN NOT MATCHED THEN
INSERT (f.id, f.name, f.state, f.notes1, f.notes2)
VALUES (id_seq.nextval, argname, argstate, NULL, NULL);
END ff_update;
/
And the sequence
create sequence ID_SEQ
start with 1000000
increment by 1
nocache;
Solution
This is expected behaviour for MERGE.
See Metalink Note: 554656.1 "Merge Increments SEQUENCE.NEXTVAL for Both Insert and Update"