I have a history table that is populated on trigger of another table and inserted data is all except 3 system columns, ID/CreatedBy/CreatedDate
Even though the ID PrimaryKey is an auto increment and the table is always populated without providing the value for ID(PK) and hence, ID will always be auto generated, we are getting primary key violation error.
Violation of PRIMARY KEY constraint 'PK_HistoryTablename'. Cannot insert duplicate key in object 'dbo.HistoryTablename'. The duplicate key value is (132137212).
What are the possible reasons for this to happen?
NOTE: For the purpose of this question, assume that the trigger query doesnt have a bug. I am looking for other reasons how it can happen at some points in time. The issue happens very rarely like once a year or so while the query runs on daily basis.
Because of popular demand, here's the sample trigger query.
ALTER TRIGGER [dbo].[tr_Tablename]
ON [dbo].[Tablename]
AFTER INSERT ,UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[HistoryTablename]
([Col1]
,[Col2]
,[Col3]
SELECT [Col1]
,[Col2]
,[Col3]
FROM Inserted
WHERE NOT (SYSTEM_USER = 'aparticularsqluser'
AND [Col2] = 'Y');
END
I really do appreciate everyone's input and i understand it may not have any straight-up answer so i am looking for what i may have missed to look at.