SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cannot insert duplicate key in object (after replication)


Cannot insert duplicate key in object (after replication)

Author
Message
tarr94
tarr94
SSC Eights!
SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)

Group: General Forum Members
Points: 863 Visits: 238
Hi,

We have a replication job that copies data from our production environment into a testing environment. As far as I can tell, the replication completed successfully, and I'm not sure the issue is being caused by the replication or not.

We have a table containing an auto-incrementing PK column that runs from "1" to "3000". However, when we try to insert a new record, we're getting a constraint violation error:

"Cannot insert duplicate key in object 'MyTable''. The duplicate key value is (1000)."

When I try running the insert again, the error changes a bit:

"Cannot insert duplicate key in object 'MyTable''. The duplicate key value is (1001)."

Clearly, there is a disconnect in which SQL Server thinks the next available Pk value much lower than it should be. How can I fix this?

I greatly appreciate any assistance!
John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)

Group: General Forum Members
Points: 129396 Visits: 18955
Looks as if either somebody is overriding the identity property by setting identity insert on, or the identity has been reseeded. Is the table in question in the publication database, or the subscription?

DBCC CHECKIDENT ('dbo.MyTable', NORESEED)


John
tarr94
tarr94
SSC Eights!
SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)

Group: General Forum Members
Points: 863 Visits: 238

Thanks for the response, John.

This is occurring in the subscription (test) DB.

After running the script you gave me, I'm getting the following message:

"current identity value '1000', current column value '3000'"

By using the following script, am I able to resolve this issue for the time being?

DBCC CHECKIDENT ('dbo.MyTable', RESEED, 3001)




John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)

Group: General Forum Members
Points: 129396 Visits: 18955
Yes, that would probably stop the errors. But you ought to find out why they're happening. Is it replication that's attempting to do the inserts? If so, either the article for that table isn't set up properly, or something's gone wrong with it. If it's user activity doing the inserts, you need to decide whether that's permitted. If it is, you're probably going to get more conflicts in the future. If it isn't, perhaps you could lock it down with permissions to prevent it?

John
tarr94
tarr94
SSC Eights!
SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)

Group: General Forum Members
Points: 863 Visits: 238
You're right, we'll need to take a closer look at what might have caused the seeding to become misaligned, but It looks like that script may have resolved our issue for the short term.

Thanks, John!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search