How to fix Violation of PRIMARY KEY constraint 'PK_Mytable'. Cannot insert duplicate key in object 'dbo.Mytable' with Transaction (Process ID 181) was deadlocked on lock resources with another process

BalajiG2000 0 Reputation points
2024-02-21T06:12:13.96+00:00

I am encountering an issue while attempting to call a stored procedure from a .NET Framework application to a Microsoft SQL Server instance. The error message returned is: "Violation of PRIMARY KEY constraint 'PK_Mytable'. Cannot insert duplicate key in object 'dbo.Mytable'. The duplicate key value is (c026da75-0528-47a9-9117-124c6690ec4f, b4f0b604-d2e2-4ce0-b3a7-1973dce6f27d, acd220ec-85a0-4999-8ebc-e0b42ec2ae07, XXX-00007449-XX). Transaction (Process ID 181) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. The statement has been terminated."
Additional Details:
The stored procedure in question is being called from a .NET Framework C# application.
The records being inserted are unique; there are no duplicate records and 100% sure about it.

Both the business logic and stored procedure do not contain any transactional scope level.
When running the stored procedure directly in SQL Server, it executes without errors and produces the expected results.
At the time of execution, my application makes only one call to the stored procedure.

                   Thank you for your assistance.
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,584 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Olaf Helper 43,896 Reputation points
    2024-02-21T06:47:58.5266667+00:00

    Cannot insert duplicate key in object 'dbo.Mytable'.

    As the message clearly says, you can not insert duplicate keys. But you provided only less information, so it's not possible to say where the bug in you code is. You have to post more details, like SQL / .NET code.


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.