programing

행이 없는 경우 Oracle 삽입

javajsp 2023. 7. 13. 20:39

행이 없는 경우 Oracle 삽입

insert ignore into table1 
select 'value1',value2 
from table2 
where table2.type = 'ok'

이를 실행하면 "missing INTO 키워드" 오류가 나타납니다.

이를 실행하면 "missing INTO 키워드" 오류가 나타납니다.

왜냐하면 INGORE는 Oracle에서 키워드가 아니기 때문입니다.이것이 MySQL 구문입니다.

MERGE를 사용할 수 있습니다.

merge into table1 t1
    using (select 'value1' as value1 ,value2 
           from table2 
           where table2.type = 'ok' ) t2
    on ( t1.value1 = t2.value1)
when not matched then
   insert values (t2.value1, t2.value2)
/

Oracle 10g부터는 두 분기를 모두 처리하지 않고 병합을 사용할 수 있습니다.9i에서는 "dummy" MATCHED 브랜치를 사용해야 했습니다.

더 오래된 버전에서 유일한 옵션은 다음과 같습니다.

  1. INSERT(또는 하위 질의)를 발행하기 전에 행의 존재 여부를 테스트합니다.
  2. PL/SQL을 사용하여 INSERT를 실행하고 DUP_VAL_ON_INDEX 오류를 처리합니다.

버전 11g 릴리스 2에서 작업할 수 있는 운이 좋다면 힌트 IGNORE_ROW_ON_DUPKEY_INDEX를 사용할 수 있습니다.

INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(table1(id)) */ INTO table1 SELECT ...

설명서에서: http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/sql_elements006.htm#CHDEGDDG

제 블로그의 예는 http://rwijk.blogspot.com/2009/10/three-new-hints.html 입니다.

안녕, 롭.

당신이 "삽입"과 "안으로" 사이에 "무시"라는 가짜 단어를 입력했기 때문입니다!

insert ignore into table1 select 'value1',value2 from table2 where table2.type = 'ok'

해야 할 일:

insert into table1 select 'value1',value2 from table2 where table2.type = 'ok'

"행이 존재하지 않는 경우 오라클 삽입"이라는 질문 제목에서 "무시"는 "이미 존재하는 경우 행을 삽입하지 않음"을 의미하는 오라클 키워드라고 생각했을 것입니다.다른 DBMS에서는 작동할 수 있지만 Oracle에서는 작동하지 않습니다.MERGE 문을 사용하거나 다음과 같이 존재를 확인할 수 있습니다.

insert into table1 
select 'value1',value2 from table2 
where table2.type = 'ok'
and not exists (select null from table1
                where col1 = 'value1'
                and col2 = table2.value2
               );

언급URL : https://stackoverflow.com/questions/3147874/oracle-insert-if-row-does-not-exist