Skip to content
Advertisement

Unique constraint violated while insert

I’m running this code

INSERT INTO ADW12_DW.WC_MFG SELECT * FROM ADW12_DW.WC_MFG_bkp;
COMMIT;

Prior to this, ADW12_DW.WC_MFG was truncated to update column size. So ADW12_DW.WC_MFG is empty before running the above code.

Advertisement

Answer

There may be duplicates in the data being added. Here is one way to limit the insert data:

INSERT INTO ADW12_DW.WC_MFG
    SELECT w.*
    FROM ADW12_DW.WC_MFG_bkp w
    WHERE w.rowid IN (SELECT MIN(w2.rowid)
                      FROM ADW12_DW.WC_MFG_bkp w2
                      WHERE w2.col = w.col
                     );

col represents what is “unique”. It may be more than one column.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement