ORA-00001: unique constraint primary key violated

杀马特。学长 韩版系。学妹 提交于 2019-12-06 14:01:41

问题


DELETE from dbo.T_LIAV_AGENT_STATE_APPROVAL SAP
WHERE EXISTS (SELECT UNIQUE 1 FROM MCS_SYNC_STATE_APPR APP

                inner join MCS_SYNC_NAME SN on SN.SE_NAME_ID = APP.SE_NAME_ID
                                    and SN.ACTION in('U','S')
                Where APP.SE_name_id = SAP.AV_NAME_ID
                and APP.SYNC_INSTANCE_ID = param_inst_ID);
COMMIT;

INSERT INTO dbo.T_LIAV_AGENT_STATE_APPROVAL

SELECT  UNIQUE 

    APP.SE_NAME_ID            AS AV_NAME_ID,
    APP.STATE                 AS AV_STATE,
    APP.APPROVAL_TYPE         AS AV_APPROVAL_TYPE,
    APP.START_DATE            AS AV_START_DATE,
    APP.END_DATE              AS AV_END_DATE,
    APP.APPOINTED             AS AV_APPOINTED,
    APP.RENEWAL_DATE          AS AV_RENEWAL_DATE,
    APP.LICENSE               AS AV_LICENSE,
    COMPANY_NAME_ID           as AV_COMPANY_CODE,
    SYSDATE                   AS TSTAMP,
    SYNC_USER_NAME_ID         AS AV_FIRST_USER_ID,
    SYSDATE                   AS AV_FIRST_DATE,
    NULL                      AS AV_LAST_USER_ID,
    NULL                      AS AV_LAST_DATE

FROM MCS_SYNC_STATE_APPR APP

WHERE exists (select 1 from t_liag_agent AG
                     where AG.ag_name_id = APP.SE_NAME_ID)
and APP.SYNC_INSTANCE_ID = param_inst_ID;

Here is the SQL part and I am getting error when I debug the code:

ORA-00001: unique constraint primary key violated At T_LIAV_AGENT_STATE_APPROVAL...

and these are the 2 columns which the primary key constraint relies on AV_NAME_ID, AV_STATE ..

There is no duplicate data as per me ...Do you think any other reason?


回答1:


If you have 'CREATE TABLE' privilege, use dbms_errlog package.

  1. Run script:

    begin
      dbms_errlog.create_error_log('DBO.T_LIAV_AGENT_STATE_APPROVAL');
    end;
    /
    
  2. Run your INSERT script with an additional clause:

    INSERT INTO dbo.T_LIAV_AGENT_STATE_APPROVAL
    SELECT ...
    FROM ...
    LOG ERRORS INTO err$_T_LIAV_AGENT_STATE_APPROVAL REJECT LIMIT UNLIMITED;
    
  3. Check error logging table for errors:

    select * from err$_T_LIAV_AGENT_STATE_APPROVAL;
    

You will see all records, that violated constraints, and error messages.




回答2:


Select statement was missing a column when compared to Insert table statement so there was this error. When i added that column to the select statement it worked perfectly.




回答3:


If you are not sure which unique constraint was violated, you can run the following SQL:

Select Distinct table_name
From all_indexes
Where index_name = 'CONSTRAINT_NAME';


来源:https://stackoverflow.com/questions/26745508/ora-00001-unique-constraint-primary-key-violated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!