Violation of PRIMARY KEY constraint on table

Naresh y 146 Reputation points
2022-04-20T15:17:10.967+00:00

Hi team

i have a table where it uses the identify column inside the table, trying to insert the records into the table getting the below error, and have the composite primary key in my table that is ,the below is the table definition,

Set identity_Insert [dbo].[Product] ON , i tried this but no use

CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[Time] ASC,
[Product] ASC

insert condition

INSERT INTO [dbo].[Product]
(
[TIME],
Product,

SELECT distinct
CONVERT(SMALLDATETIME, CONVERT(VARCHAR(10), DATEADD(DAY , -1, GETDATE()), 102)) AS [TIME] ,
Product
From dbo.stgproduct

Violation of PRIMARY KEY constraint 'PK_Product'. Cannot insert duplicate key in object 'dbo.Product'. The duplicate key value is (Apr 19 2022 12:00AM, BACN10LJ-P4S7-2).
The statement has been terminated.

Developer technologies Transact-SQL
SQL Server Other
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2022-04-20T15:35:34.297+00:00

    It is telling you, dbo.Product already has a value for (Apr 19 2022 12:00AM, BACN10LJ-P4S7-2).

    You need to look at dbo.Product.

    0 comments No comments

  2. Naresh y 146 Reputation points
    2022-04-20T15:50:00.537+00:00

    No the table is empty, it doesn't have that value,issue with the composite key or the identity column ,please suggest


  3. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-04-20T16:43:04.55+00:00

    Your code for the Time column are creating duplicate values.
    Try this:

    INSERT INTO [dbo].[Product]
    (
    [TIME],
    Product)

    SELECT distinct
    CONVERT(VARCHAR(24), DATEADD(DAY , -1, GETDATE()), 126) AS [TIME] ,
    Product
    From dbo.stgproduct


  4. Tom Phillips 17,771 Reputation points
    2022-04-20T17:36:34.42+00:00

    Run this to find the duplicate records:

    ;with dup as (
    SELECT distinct
    CONVERT(SMALLDATETIME, CONVERT(VARCHAR(10), DATEADD(DAY , -1, GETDATE()), 102)) AS [TIME] ,
    Product
    From dbo.stgproduct
    )
    SELECT [TIME],[Product]
    FROM dup
    GROUP BY [TIME],[Product]
    HAVING COUNT(*) > 1
    
    0 comments No comments

  5. LiHong-MSFT 10,056 Reputation points
    2022-04-21T03:14:37.89+00:00

    Hi @Naresh y
    First,to make sure the [Product] table is empty,please run this query:

    Truncate table [dbo].[Product]  
    

    Second, run this Insert query:

    INSERT INTO [dbo].[Product]( [TIME],Product)  
    SELECT distinct CONVERT(SMALLDATETIME, CONVERT(VARCHAR(10), DATEADD(DAY , -1, GETDATE()), 102)) AS [TIME] , Product  
    From dbo.stgproduct  
    

    Below is my local test which is able to insert data successfully:

    create table test(id int identity(1,1),  
                      times varchar(30),employeeid int,  
                      constraint PK_Product primary key CLUSTERED (times ASC, employeeid ASC))  
    --Truncate table test  
      
    insert into test(times,employeeid)  
    SELECT distinct CONVERT(SMALLDATETIME, CONVERT(VARCHAR(10), DATEADD(DAY , -1, GETDATE()), 102)) AS [TIME]  ,employee_id  
    From employees  
      
    --drop table test  
    

    If still receive this error,please feel free to let us know.

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.