Quantcast
Channel: SQL Server High Availability and Disaster Recovery forum
Viewing all articles
Browse latest Browse all 4689

Insert failed due to fk conflicts.

$
0
0

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'.


Viewing all articles
Browse latest Browse all 4689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>