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.
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?
Edit: I realised I probably don't want to do this anyway, but I'm still curious as to whether it's possible or not.
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
, as demonstrated in this thread on askTom,DBMS_ERRLOG
(available since 10g I think).The second method is all automatic, here's a demo (using 11gR2):
You can use the
LOG ERROR
clause withINSERT ALL
(thanks @Alex Poole), but you have to add the clause after each table:Use the MERGE statement to handle this situation: