Sql-server – SSIS Failed foreign key constraint

foreign keysql serverssdtssis

I'm loading 2 csv files into 2 tables in SQL Server Data Tools using SSIS, and the 2nd one is connected to the 1st via a foreign key constraint. The problem is that a row of the 2nd table has a key that is missing from the 1st table, so the foreign key constraint fails and an error message is presented.

Is there a way to check the foreign key constraint before insertion and catch the error so that it doesn't interrupt the insertion of the rest of the rows?

Best Answer

IN SSIS, you can probably set up a lookup step with the foreign key data from table 1; rows that don't match could be routed into an error file, and the rest routed into the table.

Alternately, load the data into a staging table without the foreign key constraint, and run a script/stored procedure to validate the data in the staging table, including making sure the foreign keys are present.