H,
A trigger is fired after an insert action on a table that has an identity column, and the trigger inserts into another table @@IDENTITY will return the identity value of the first insert.
But @@IDENTITY returns wrong value so my insertion is failed because @@IDENTITY value is foreign key in another table.
My question is why @@IDENTITY gives wrong value.
ex. insert into tbl1(clo1,col2,col3)values(1,2,3)
after inserting into this table trigger is fired
ex. CREATE TRIGGER [dbo].[trigg] ON [dbo].[tbl1] AFTER INSERT
AS
BEGIN
INSERT INTO tabl2
(col11,col22, col33,col44)
VALUES ('Yes','Yes','Yes',@@IDENTITY) --- Here @@IDENTITY gives wrong value that not present in tbl1
END
error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl1_tbl2". The conflict occurred in database "DB", table "dbo.tbl1", column 'PK_ID'.