Oracle sql merge to insert and delete but not update -
is there way use oracle merge insert , delete not update?
i have table representing set of values related single row in table. change set of values deleting them , adding new set, or selectively deleting , adding others, interested in making single statement if possible.
here working example update. in order make work, had add dummy
column available update not in on
condition. there way delete , insert without dummy column update?
no column on
condition may in update set
list if not updated.
create table every_value ( the_value varchar2(32) ); create table paired_value ( the_id number, a_value varchar2(32) , dummy number default 0 ); -- the_id foreign_key row in table insert every_value ( the_value ) values ( 'aaa' ); insert every_value ( the_value ) values ( 'abc' ); insert every_value ( the_value ) values ( 'ace' ); insert every_value ( the_value ) values ( 'adg' ); insert every_value ( the_value ) values ( 'aei' ); insert every_value ( the_value ) values ( 'afk' ); -- pair ace , afk id 3 merge paired_value p using every_value e on ( p.the_id = 3 , p.a_value = e.the_value ) when matched update set dummy=dummy+1 delete a_value not in ('ace','afk') when not matched insert (the_id,a_value) values (3,e.the_value) e.the_value in ('ace','afk'); -- pair ace , aei id 3 -- should remove afk, add aei, nothing ace merge paired_value p using every_value e on ( p.the_id = 3 , p.a_value = e.the_value ) when matched update set dummy = dummy+1 delete a_value not in ('ace','aei') when not matched insert (the_id,a_value) values (3,e.the_value) e.the_value in ('ace','aei'); -- pair aaa , adg id 4 merge paired_value p using every_value e on ( p.the_id = 4 , p.a_value = e.the_value ) when matched update set dummy = dummy+1 delete a_value not in ('aaa','adg') when not matched insert (the_id,a_value) values (4,e.the_value) e.the_value in ('aaa','adg'); select * paired_value;
i have tried in oracle 10g and, sqlfiddle, oracle 11g.
no, cannot delete rows have not been updated merge command.
here documentation: http://docs.oracle.com/cd/b28359_01/server.111/b28286/statements_9016.htm
specify delete where_clause clean data in table while populating or updating it. the rows affected clause rows in destination table updated merge operation. delete condition evaluates updated value, not original value evaluated update set ... condition. if row of destination table meets delete condition not included in join defined on clause, not deleted. delete triggers defined on target table activated each row deletion.
that means, rows must updated. hovewer, don't need update rows, after update use same clause using after delete
when matched update set dummy=dummy a_value not in ('ace','afk') delete a_value not in ('ace','afk')
Comments
Post a Comment