I have the following SQL Code to Insert Data From One table to Another by comparing the Data in two table. The PK in the destination table are [bomItem], [bomRev], [bomEntry]
.
I want to Insert if ItemID in source table = bomItem field in Dest. table
AND [rev](field in Source tbl)NOT=[bomRev](in destination tbl)
.
When I tried to run the script i get the following error
Msg 547, Level 16, State 0, Line 46 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_MIBOMD_MIBOMH". The conflict occurred in database "MITESTCO", table "dbo.MIBOMH". The statement has been terminated.
USE [MITESTCO];
GO
INSERT INTO [MIBOMD]
([bomItem], [bomRev], [bomEntry], [partId], [qty],[lead])
SELECT [ItemID], [rev], [bomEntry], [partid], [qty],[lead]
FROM [assy]
WHERE [rev] IN (SELECT [bomRev] FROM [MIBOMH])
AND [ItemID] IN (SELECT [bomItem] FROM [MIBOMH])
AND [ItemID] IN (SELECT [ItemID] FROM [MIITEM])
AND [partid] IN (SELECT [ItemID] FROM [MIITEM]);