Replication-Replication Distribution Subsystem: agent INSTANCE-db-Publication-SUBSCRIBER_INSTANCE-21 failed. Violation of PRIMARY KEY constraint 'PK_Whatever'. Cannot insert duplicate key in object 'dbo.table1'
The error message simply says that replication between the publisher and the subscriber could not push thru due to an existing record in the subscription that is causing a violation of the PRIMARY KEY constraint. Either a record is manually inserted in the subscriber or its just an annoying application which is not properly configured. Here's how you can fix this
- Open Replication Monitor and check for the publication that is causing the error. You should see some error messages in the details that display something about the transaction sequence number, publisher id and command id values
- Next, you retrieve the command associated with the transaction sequence number using the system stored procedure sp_browsereplcmds. This will return a result set of the replicated commands stored in the Distribution database at the Distributor. The query should look something like this
- Next, run a SELECT query on the subscriber instance against the table specified in the subscription passing the values you retrieved from the sp_browsereplcmds system stored procedure.
- If the record already exists, simply delete the record since it will be written over by the replication (of course, this is not always the case and would depend on your business requirements)
EXEC Distribution..sp_browsereplcmds @xact_seqno_start = '0x000B5103000043B7000700000000' ,@xact_seqno_end = '0x000B5103000043B7000700000000',@command_id =2 , @publisher_database_id = 1
This will give you an idea on the possible INSERT/UPDATE/DELETE statement that is inside this transaction. Based on the error message, we are looking at either an INSERT or an UPDATE statement.
This is just an overview of what you can do when you see such errors in replication. There are a few more articles and tips to help you get by from this website by Paul Ibison
3 comments:
Any idea what causes this problem in the first place?
In my case:
- I got 2000 db hosted in SQL 2005 standard
- I change the database version to 2005
- setup a Transactional Replication from SRV1 to SRV2 (only insert and update, do not deliver delete)
- snapshot replicated succesfully
- transaction failed due to Primary key constraint violation
I found that the update statement is never used, the transactions transfered are using delete and insert statements.
For the fix for "Edwin Yosorahardjo" with his primary key violation. It might be caused by triggers firing on the subscriber database. This is what was occuring for me
Post a Comment