Skip to content
Advertisement

MERGE statement conflicts with foreign key constraint

I have two tables. STCH is one to many to SSBT. TN in STCH is a foreign key in SSBT. SSBT is a list of assignments by TN. I want to keep STCH (employee list) updated with changes and am using a MERGE Statement.

The MERGE statement pulls data from a similar table in another database and populates my STCH table in the assignments database:

This is my error message

Msg 547, Level 16, State 0, Line 1 The MERGE statement conflicted with the REFERENCE constraint “FK_87”. The conflict occurred in database “SUBMAN”, table “dbo.SSBT”, column ‘TN’. The statement has been terminated.

When I run the subquery by itself I get correct data. TN,TE,ID,U1

Here is the Foreign Key

Advertisement

Answer

That means that the data you’re deleting (by your WHEN NOT MATCHED BY SOURCE THEN DELETE;) is referenced on your other table. Remove that part of the query if you don’t want to delete data that is not in your Source subquery.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement