Oracle #39;INSERT ALL#39; ignore duplicates(Oracle INSERT ALL 忽略重复项)
问题描述
我有一个带有唯一约束的数据库表(唯一的 (DADSNBR, DAROLEID) 对).我将同时向该表中插入多个值,所以我想使用一个查询来完成它 - 我假设这将是更快的方法.因此,我的查询是:
I have a database table with a unique constraint on it (unique (DADSNBR, DAROLEID) pair). I am going to be inserting multiple values into this table simultaneously, so I'd like to get it done using one query - I'm assuming this would be the faster way. My query is thus:
INSERT ALL
INTO ACCESS (DADSNBR, DAROLEID) VALUES (68, 1)
INTO ACCESS (DADSNBR, DAROLEID) VALUES (68, 2)
INTO ACCESS (DADSNBR, DAROLEID) VALUES (68, 3)
INTO ACCESS (DADSNBR, DAROLEID) VALUES (68, 4)
SELECT 1 FROM DUAL
由于语句中的某些条目与数据库中已有的条目重复,因此整个插入失败并且没有插入任何行.
Since there are some entries within the statement that are duplicates of those already in the database, the whole insert fails and none of the rows are inserted.
有没有办法忽略唯一约束失败的情况,只插入唯一约束,而不必将其拆分为单独的 INSERT 语句?
Is there a way to ignore the cases where the unique constraint fails, and just insert the ones that are unique, without having to split it up into individual INSERT statements?
我意识到我可能不想这样做,但我仍然很好奇这是否可能.
I realised I probably don't want to do this anyway, but I'm still curious as to whether it's possible or not.
推荐答案
在 Oracle 中,语句要么完全成功,要么完全失败(它们是原子的).但是,您可以在某些情况下添加子句来记录异常而不是引发错误:
In Oracle, statements either succeed completely or fail completely (they are atomic). However, you can add clauses in certain cases to log exceptions instead of raising errors:
- 使用
BULK COLLECT - SAVE EXCEPTIONS,如 这个帖子在 askTom 上, - 或使用
DBMS_ERRLOG(我认为从 10g 开始可用).
第二种方法是全自动的,这里有一个demo(使用11gR2):
The second method is all automatic, here's a demo (using 11gR2):
SQL> CREATE TABLE test (pk1 NUMBER,
2 pk2 NUMBER,
3 CONSTRAINT pk_test PRIMARY KEY (pk1, pk2));
Table created.
SQL> /* Statement fails because of duplicate */
SQL> INSERT into test (SELECT 1, 1 FROM dual CONNECT BY LEVEL <= 2);
ERROR at line 1:
ORA-00001: unique constraint (VNZ.PK_TEST) violated
SQL> BEGIN dbms_errlog.create_error_log('TEST'); END;
2 /
PL/SQL procedure successfully completed.
SQL> /* Statement succeeds and the error will be logged */
SQL> INSERT into test (SELECT 1, 1 FROM dual CONNECT BY LEVEL <= 2)
2 LOG ERRORS REJECT LIMIT UNLIMITED;
1 row(s) inserted.
SQL> select ORA_ERR_MESG$, pk1, pk2 from err$_test;
ORA_ERR_MESG$ PK1 PK2
--------------------------------------------------- --- ---
ORA-00001: unique constraint (VNZ.PK_TEST) violated 1 1
您可以将 LOG ERROR 子句与 INSERT ALL 一起使用(感谢 @Alex Poole),但是你必须在每个表之后添加子句:
You can use the LOG ERROR clause with INSERT ALL (thanks @Alex Poole), but you have to add the clause after each table:
SQL> INSERT ALL
2 INTO test VALUES (1, 1) LOG ERRORS REJECT LIMIT UNLIMITED
3 INTO test VALUES (1, 1) LOG ERRORS REJECT LIMIT UNLIMITED
4 (SELECT * FROM dual);
0 row(s) inserted.
这篇关于Oracle 'INSERT ALL' 忽略重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 'INSERT ALL' 忽略重复项
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
