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' 忽略重复项
基础教程推荐
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01