I’m trying to load the below table using SQL. In my Ldirectory table I have the combination of SY and LIDENTIFIER as primary key and I’m trying to run this query
insert into S_User.LDIRECTORY (SY,LIDENTIFIER,ONAME,TELNUMBER)
select 2013,D.CODE, D.NAME, D_YEAR.PHONE_NUMBER
from WHS.D WHS.D_YEAR
where WHS.D.D_KEY=WHS.D_YEAR.D_KEY
and the error that I’m receiving is:
SQL Error ORA-00001 unique constraint violated
Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
Action: Either remove the unique restriction or do not insert the key.
How can I solve this issue? I think during the insertion its treating the SY column as primary key itself but I have just one same value for that column.
The error message should indicate the name of the unique constraint that was violated. Assuming the constraint in question is the primary key rather than some other unique constraint defined on the table and assuming that the primary key constraint is a composite constraint defined on the combination of
SYandLIDENTIFIERas you indicate, that implies that your query is returning duplicate rows.The query you are running returns a hard-coded value of 2013 for
SY. So you would expect the constraint to be violated if there are any two rows in the result whereD.CODEare the same. Are you certain that you expectD.CODEto be unique across the entire result set? It’s hard to guess based on the names of the objects (no idea whatDmight represent, no idea whyD_YEARwould have aPHONE_NUMBERcolumn, etc) but I would tend to guess that one row inDmight map to multiple rows inD_YEARin which case there would be multiple rows in the result that had the sameD.CODEvalue thus violating the constraint.If you are convinced that the query should not return any two rows with the same
D.CODEvalue, you could use DML error logging to write the rows that violate the constraint to an error table so that you could analyze the problem.