1

我在Oracle Equivalent to MySQL INSERT IGNORE上找到了一个非常相似的主题? 但是,我无法使用任何建议的解决方案。我的情况有点特殊,因为我的表只包含 1 个字段,即主键。让我们在下面调用字段“id”和表“myTable”。

使用合并

merge into myTable t1 from (
    select 42 as the_pk_value, 'TEST' as some_column from dual
) t2 on (t1.id = t2.the_pk_value)
when not matched then 
    insert (id) values (t2.the_pk_value);

第一次尝试会给出SQL Error: ORA-02012: missing USING keyword错误消息。我不得不承认我不理解建议的语法,所以也许我在适应我的 pk-only-table 时搞砸了一些东西。但是由于显示的示例没有使用任何USING关键字,我不明白我的错误可能在哪里。

使用提示

insert /*+ ignore_row_on_dupkey_index(SPSECU, PK_SPSECU) */ into myTable (id) values ('TEST');

这确实可以通过 SQL Developer 工作,但是它不适用于 Java(我怀疑 OJDBC 驱动程序会删除注释以减少传输大小。顺便说一句,我无法弄清楚我正在使用哪个 JDBC 驱动程序...... Spring Tool Source bundle 似乎无需任何进一步配置即可连接到 Oracle 数据库。我只看到安装了 DERBY 默认驱动程序。

使用不存在

我无法使这种语法起作用。这是我写的:

insert into myTable (id) values ('TEST') where not exists (select id from myTable where id='TEST');

我收到SQL Error: ORA-00933: SQL command not properly ended这个版本的错误。

使用插入选择

我对 knagaev 提出的解决方案一无所知……试图将其适应我的桌子给了我这个:

insert into myTable t1 select id from myTable t2 where not exists (select 1 from t1 where t1.id = 'TEST');

有人能帮我吗 ?我习惯于 MySQL INSERT IGNORE 简单的语法,并且在 Oracle 上很新(使用 11g 版本)。

4

1 回答 1

1

合并使用using而不是from

merge into myTable t1 USING (
    select 42 as the_pk_value, 'TEST' as some_column from dual
) t2 on (t1.id = t2.the_pk_value)
于 2013-05-07T08:50:10.647 回答