Violation of Primary Key error on Identity column

纵然是瞬间 提交于 2019-12-01 17:17:37

I ran into this and much like user3170349, it was a seed issue on the column. I'm adding some additional info, however.

First, you can run this to figure out if you have a seed problem:

DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', NORESEED);

This will give you information which will read something like this:

Checking identity information: current identity value 'XXXX', current column value 'YYYY'.

If YYYY is larger than XXXX, then you have a problem and need to RESEED the table to get things going again. You can do so with the following command:

DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', RESEED, ZZZZZ);

Where ZZZZ is the reseed value. That value should be at least one higher than YYYY. YMMV, so pick a value that is appropriate for your situation.

"Code in question has been running for over 5 years." "It might insert 1000 records at a time or it might insert 50,000 "

Is it possible you have finally overflowed the integer type of the primary key?

Did it wrap around and is now starting over? That would cause duplicate primary keys.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!