Hello, I have a web production system that has had this problem twice now. This logic is run hundreds of times a day for each user as it is part of the application dashboard and this has only occurred twice since May.
I am creating a temporary table that is loaded with the result set of a Select/Union that fails when I try to create a unique key on it. As far as I can tell, the rows should be mutually exclusive and the selected key: EVENT_ID is a unique identity primary key column and all of the referenced columns in the SQL below are defined as NOT NULL.
My only explanation is that an update was applied between the 2 union sides that caused the same row to be selected on both sides. I checked the key entries referenced in the duplicate key errors and on both occasions the row had been updated at virtually the same time as the duplicate key error.
Here is the SQL:
drop table tmp_Martinator_empqueueevents ; create table dbo.tmp_Martinator_empqueueevents ( EVENT_ID integer) ; insert into tmp_Martinator_empqueueevents SELECT EVENT.EVENT_ID FROM EVENT with (nolock) WHERE EVENT.DATA_IMPORT_LOG_ID >= 0 AND EVENT.STATUS_CODE_ID in (283, 284) AND EVENT.ASSIGNED_EMP_ID = 97 union SELECT EVENT.EVENT_ID FROM EVENT with (nolock) WHERE EVENT.DATA_IMPORT_LOG_ID >= 0 AND EVENT.EVENT_TYPE_ID = 293 AND EVENT.STATUS_CODE_ID = 285 AND EVENT.REVIEWED_FLAG = 0 AND EVENT.ASSIGNED_EMP_ID = 97 ; create unique index event_extract_pk on tmp_Martinator_empqueueevents ( EVENT_ID ) ;
I don't see how the same row could end up on both sides of this union and I am running Read Committed so that updates should not interfere with this either.
Any ideas or tips would be greatly appreciated, thank you.