I want the package to insert a large amount of generated data that has a cyclic dependency (the column in each table is a foreign key limited to another table). To get around this, I just want to disable foreign key constraints, insert data, and enable constraints again.
Google, I found many solutions, but none of them worked. Right now I have:
ALTER TABLE TableName NOCHECK CONSTRAINT ALL
The command starts and does not produce any errors, but when I try to clear the table in preparation for inserting data, I get the following error:
System.Data:0:in `OnError': The DELETE statement conflicted with the REFERENCE constraint "FK_1_2_ConstraintName". The conflict occurred in database "DatabaseName", table "dbo.SomeOtherTable", column 'PrimaryKey'.\r\nThe statement has been terminated.\r\nChecking identity information: current identity value '0', current column value '0'.\r\nDBCC execution completed. If DBCC printed error messages, contact your system administrator. (System::Data::SqlClient::SqlException)
My current theory is that this is caused by a foreign key constraint in another table, which depends on the table being modified.
There are two solutions that I can solve with this problem:
Go through all the table dependencies on the table into which I insert and disable foreign key constraints. It seems overly complicated.
Disable foreign key restrictions for all tables in the database.
Any solution will work, but I'm not sure where to start in any solution. Any ideas?
source share