oracle insert if row does not exists

后端 未结 3 511
盖世英雄少女心
盖世英雄少女心 2020-11-30 10:30
insert ignore into table1 
select \'value1\',value2 
from table2 
where table2.type = \'ok\'

When I run this I get the error \"missing INTO keyword

3条回答
  •  日久生厌
    2020-11-30 10:58

    Because you typed the spurious word "ignore" between "insert" and "into"!!

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

    Should be:

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

    From your question title "oracle insert if row not exists" I assume you thought "ignore" was an Oracle keyword that means "don't try to insert a row if it already exists". Maybe this works in some other DBMS, but it doesn't in Oracle. You could use a MERGE statement, or check for existence like this:

    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
                   );
    

提交回复
热议问题