Link to home
Start Free TrialLog in
Avatar of ppmb
ppmb

asked on

Primary Key Violation

I have a table with Identity(1,1) as Primary Key and been using this table for some months now. All of a sudden, I got the following error message from my app (VB5) saying that there's a primary key violation :
"[Microsoft][ODBC SQL Server Driver][SQL Driver] Violation of PRIMARY KEY constraint 'PK_Staff_Work_Hours_2__78'. Attempt to insert duplicate key in object 'Staff_Work_Hours' (#2627) [Microsoft][ODBC SQL Server Driver][SQL Server] Command has been aborted. (#2721) "
How did that happen?? What is the cause of it?? and how can I fix it???
ASKER CERTIFIED SOLUTION
Avatar of lmorris
lmorris

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ppmb
ppmb

ASKER

When you say "shutting down" the SQL Server 6.5, do you mean the SQL Server itself being shut down or the NT Server is being shut down??? Because both situations happen from time to time. DBA of SQL Server will kick out everyone when it is doing a gigantic job and then restarts the SQL Server. Sometimes the NT crashes and will be restarted. So under what situation the identity column will be screwed up??? Thanks!
Avatar of ppmb

ASKER

Forgot to ask you this:
Does dbcc automatically fix the error existed in the identity column???
Thanks!
No, this will not fix an error. The proposed answer does not solve your problem, and you may feel free to reject it.

This is a very well-known problem, and it cannot be solved with DBCC, because it doesn't cause logical corruption of table structure.
We had this problem a big way, and developed a workaround against it.
Actually, you MIGHT fix it with DBCC checkident once, but it will surely re-appear some time later. So, if you only care about just fixing it once, by all means, you should accept lmorris's answer, because it probably will solve the problem once, but it fixes only the consequence, not the cause.
Avatar of ppmb

ASKER

So ,mitek, what suggestion do you have if I insist in using Identity as Primary Key?