it-source

Oracle sql 병합을 삽입 및 삭제하지만 업데이트하지 않음

criticalcode 2023. 10. 28. 07:59
반응형

Oracle sql 병합을 삽입 및 삭제하지만 업데이트하지 않음

Oracle merge를 사용하여 삽입 및 삭제를 하되 업데이트는 하지 않는 방법이 있습니까?

저는 다른 표에 있는 단일 행과 관련된 값들의 집합을 나타내는 표를 가지고 있습니다.값의 집합을 모두 삭제하고 다시 추가하거나, 일부를 선택적으로 삭제하고 추가하는 방법으로 변경할 수 있지만, 가능하다면 하나의 문장으로 만드는 것에 관심이 있습니다.

다음은 업데이트와 관련된 작업 예시입니다.이 작업을 수행하기 위해서는 추가해야 했습니다.dummy에 없는 열을 업데이트할 수 있도록 하기 위해on조건.업데이트할 더미 열 없이 삭제하고 삽입만 할 수 있는 방법이 있습니까?

에서 열이 없습니다.on조건이 있을 수도 있습니다.update set실제로 업데이트되지 않더라도 목록에 표시합니다.

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 is a foreign_key to a row in another table

insert into every_value ( the_value ) values ( 'aaa' );
insert into every_value ( the_value ) values ( 'abc' );
insert into every_value ( the_value ) values ( 'ace' );
insert into every_value ( the_value ) values ( 'adg' );
insert into every_value ( the_value ) values ( 'aei' );
insert into every_value ( the_value ) values ( 'afk' );

-- pair ace and afk with id 3
merge into paired_value p using every_value e
on ( p.the_id = 3 and p.a_value = e.the_value )
when matched then update set dummy=dummy+1
delete where a_value not in ('ace','afk')
when not matched then insert (the_id,a_value)
values (3,e.the_value)
where e.the_value in ('ace','afk');

-- pair ace and aei with id 3
-- should remove afk, add aei, do nothing with ace
merge into paired_value p using every_value e
on ( p.the_id = 3 and p.a_value = e.the_value )
when matched then update set dummy = dummy+1
delete where a_value not in ('ace','aei')
when not matched then insert (the_id,a_value)
values (3,e.the_value)
where e.the_value in ('ace','aei');

-- pair aaa and adg with id 4
merge into paired_value p using every_value e
on ( p.the_id = 4 and p.a_value = e.the_value )
when matched then update set dummy = dummy+1
delete where a_value not in ('aaa','adg')
when not matched then insert (the_id,a_value)
values (4,e.the_value)
where e.the_value in ('aaa','adg');

select * from paired_value;

저는 오라클 10g에서 이것을 시도해보았고, 이 sqlfiddle로 오라클 11g을 시도해보았습니다.

아니요, merge 명령으로 업데이트되지 않은 행은 삭제할 수 없습니다.
다음은 문서입니다: http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm

DELETE where_clause를 지정하여 테이블의 데이터를 채우거나 업데이트하는 동안 해당 데이터를 정리합니다.이 조항의 영향을 받는 행은 병합 작업에 의해 업데이트되는 대상 테이블의 행뿐입니다.DELETE WHERE 조건은 UPDATE SET...에서 평가한 원래 값이 아니라 업데이트된 값을 평가합니다.WHERE 조건.대상 테이블의 행이 DELETE 조건을 충족하지만 ON 절에서 정의한 조인에 포함되지 않으면 삭제되지 않습니다.각 행 삭제 시 대상 테이블에 정의된 모든 삭제 트리거가 활성화됩니다.

즉, 행을 업데이트해야 합니다.그러나 UPDATE 후에는 DELETE 후에 사용하는 WHERE 절과 동일한 WHERE 절을 사용하므로 모든 행을 업데이트할 필요는 없습니다.

when matched then update set dummy=dummy
    where a_value not in ('ace','afk')
delete 
    where a_value not in ('ace','afk')

열을 자체로 설정할 수 있습니다.

MERGE ...
WHEN MATCHED THEN 
   UPDATE SET a_value = a_value WHERE a_value not in ('ace','afk')
   DELETE WHERE a_value not in ('ace','afk')

따라서 더미 열이 필요 없습니다.

언급URL : https://stackoverflow.com/questions/17709602/oracle-sql-merge-to-insert-and-delete-but-not-update

반응형