Skip to main content
Known Participant
May 11, 2009
Question

how to fix this error

  • May 11, 2009
  • 3 replies
  • 1806 views

Hi i am getting this error when i try to upload some file how can i solve this error in database

[Macromedia][SQLServer JDBC Driver][SQLServer]Violation of PRIMARY KEY constraint 'PK_di_audit_corp_upc_tbl'. Cannot insert duplicate key in object 'dbo.di_audit_corp_upc_tbl'.

This topic has been closed for replies.

3 replies

Inspiring
May 11, 2009

You could use a SQL stored procedure to handle your inserts.  By including logic within your stored procedure to avoid inserting duplicate rows you avoid the primary key related exception you are seeing.

cfnewAuthor
Known Participant
May 11, 2009

i had insert login in stored procedure and what logic had to be used to avoid this error in stored procedure my insert logic in stored procedure is :


        INSERT INTO dbo.di_audit_corp_upc_tbl (
            rcp_dpt_cd
            , cpt_dpt_cd
            , cpt_com_cd
            , sub_com_cd
            , cas_upc_no
            , con_upc_no
            , con_upc_tx
            , pid_lng_dsc_tx
            , pid_sht_dsc_tx
            , aut_vld_cd
            , rev_by
            , rev_dt
        ) VALUES (
            '07'
            , @cpt_dpt_cd
            , @cpt_com_cd
            , @sub_com_cd
            , '0000000000000'
            , @con_upc_no
            , @pid_lng_dsc_tx
            , @pid_lng_dsc_tx
            , @pid_sht_dsc_tx
            , 'N'
            , @rev_by
            , @rev_dt
        )

Inspiring
May 11, 2009

I use the approach of inserting the record unless it's already there.  Instead of this:

insert into mytable (pkfield, field1, etc)

values (1, 2, etc)

I do this

insert into mytable (pkfield, field1, etc)

select distinct 1, 2, etc

from SomeSmallTable

where (select count(*) from mytable where pkfield = 1 ) = 0

However, if you are processing a large text files, and it includes existing primary key values, maybe you are supposed to update those records.

ilssac
Inspiring
May 11, 2009

kirannaga wrote:

Hi i am getting this error when i try to upload some file how can i solve this error in database

Don't try to load a new record into the database with the same value for in the primary key field.

How you fix this either by changing the data to remove the duplicate key value(s) or changing the database to not enforce unique key constraint on the field depends on which way you want to go and which would break your system the least.

Michael Borbor
Inspiring
May 11, 2009

Your query is trying to insert a duplicated primary key to the db. How

do you manage the creation of new pk in that table.

Sincerely,

Michael

El 11/05/2009, a las 11:15, kirannaga <forums@adobe.com> escribió:

>

Hi i am getting this error when i try to upload some file how can i

solve this error in database

>

[SQLServer JDBC Driver][SQLServer]Violation of PRIMARY

KEY constraint 'PK_di_audit_corp_upc_tbl'. Cannot insert duplicate

key in object 'dbo.di_audit_corp_upc_tbl'.

>

cfnewAuthor
Known Participant
May 11, 2009

i dont know it has duplicate because i had csv file with5000 records so i am not able to find that value where it is,do we have any other option to avoid this ?

Michael Borbor
Inspiring
May 11, 2009

Execute a query to verify the existence and if it does exist then you

could change the PK or let the user know or something.

Sincerely,

Michael

El 11/05/2009, a las 12:00, kirannaga <forums@adobe.com> escribió:

>

i dont know it has duplicate because i had csv file with5000 records

so i am not able to find that value where it is,do we have any other

option to avoid this ?

>