Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 396 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
Merge stmt trying to insert duplicate while the ON clause is based on index

745400
Member Posts: 5
Hi,
I am using a merge statement to insert record in a table. The table has an unique index based on four columns. The merge statement matches on all four column (a.column1 = b.column1 and a.column2 = b.column2 ... etc.) in the ON clause. If a match is found then it does nothing if not found corresponding record is inserted.
But for some reason it is trying to insert duplicate and I am getting unique constraint violated exception.
The database is huge and the source table contains more than 200,000 records.
Any idea how it can happen?
I am using a merge statement to insert record in a table. The table has an unique index based on four columns. The merge statement matches on all four column (a.column1 = b.column1 and a.column2 = b.column2 ... etc.) in the ON clause. If a match is found then it does nothing if not found corresponding record is inserted.
But for some reason it is trying to insert duplicate and I am getting unique constraint violated exception.
The database is huge and the source table contains more than 200,000 records.
Any idea how it can happen?
Best Answer
-
How can it happen?
Easily if there is a duplicate in the data you want to merge in, bearing in mind that this is a set-based operation with the two sets of data evaluated using the standard read consistency model at the start of the query.Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> DROP TABLE t1; Table dropped. SQL> SQL> CREATE TABLE t1 2 (col1 NUMBER PRIMARY KEY); Table created. SQL> SQL> MERGE 2 INTO t1 3 USING (SELECT 1 x 4 FROM DUAL 5 UNION ALL 6 SELECT 1 7 FROM DUAL) x 8 ON (x.x = t1.col1) 9 WHEN NOT MATCHED THEN 10 INSERT 11 VALUES 12 ( x.x); MERGE * ERROR at line 1: ORA-00001: unique constraint (ROS.SYS_C0088944) violated SQL> rollback; Rollback complete. SQL> MERGE 2 INTO t1 3 USING (SELECT DISTINCT x 4 FROM ( 5 SELECT 1 x 6 FROM DUAL 7 UNION ALL 8 SELECT 1 9 FROM DUAL)) x 10 ON (x.x = t1.col1) 11 WHEN NOT MATCHED THEN 12 INSERT 13 VALUES 14 ( x.x); 1 row merged. SQL>
Edited by: DomBrooks on Jan 15, 2010 9:36 AM
Answers
-
The database is huge and the source table contains more than 200,000 records.Any bug will not depend upon the number of rows.
Post Operating System (OS) name & version for DB server system.
Post results of
SELECT * from v$version
It would be helpful if you provided DDL for tables involved.
It would be helpful if you provided DML for test data.
It would be helpful if you provided the SQL that produces reported results so we reproduce what you claim. -
Maybe you have more than one unique constraint (or index) on this table?
Aside from guessing, you'd have to provide a lot more information as previously mentioned. -
How can it happen?
Easily if there is a duplicate in the data you want to merge in, bearing in mind that this is a set-based operation with the two sets of data evaluated using the standard read consistency model at the start of the query.Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> DROP TABLE t1; Table dropped. SQL> SQL> CREATE TABLE t1 2 (col1 NUMBER PRIMARY KEY); Table created. SQL> SQL> MERGE 2 INTO t1 3 USING (SELECT 1 x 4 FROM DUAL 5 UNION ALL 6 SELECT 1 7 FROM DUAL) x 8 ON (x.x = t1.col1) 9 WHEN NOT MATCHED THEN 10 INSERT 11 VALUES 12 ( x.x); MERGE * ERROR at line 1: ORA-00001: unique constraint (ROS.SYS_C0088944) violated SQL> rollback; Rollback complete. SQL> MERGE 2 INTO t1 3 USING (SELECT DISTINCT x 4 FROM ( 5 SELECT 1 x 6 FROM DUAL 7 UNION ALL 8 SELECT 1 9 FROM DUAL)) x 10 ON (x.x = t1.col1) 11 WHEN NOT MATCHED THEN 12 INSERT 13 VALUES 14 ( x.x); 1 row merged. SQL>
Edited by: DomBrooks on Jan 15, 2010 9:36 AM -
Yes. Actually duplicates in one of the sets was issue.
This discussion has been closed.