0

I want to insert without deleting a foreign key constraint. I have 2 tables, table1 and table2. When I try to insert into table1, I get something along the following error:

The insert statement conflicted with the foreign key constraint "fk_dbo.table1.dbo.table2id"'

This error occurs even after I use ALTER TABLE ? NOCHECK CONSTRAINT ALL

So basically I want to insert values into some foreign key columns without deleting the foreign key constraints. Is there a way to do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Goku
  • 1,565
  • 1
  • 29
  • 62
  • 7
    The way to do it is to insert a corresponding value into the parent table first, then insert into the child table - that satisfies the constraint and retains the referential integrity of your database. – Turophile Feb 11 '16 at 22:56
  • @Turophile the thing is im using ssis and i have lots of tables with correct data in them. I know the data is correct. I want to insert these keys in an order that is convenient to me. (It is going to be a huge task to figure out the perfect order to populate all tables according to key constraints) – Goku Feb 12 '16 at 15:10
  • In that case, see this question: http://stackoverflow.com/questions/737115/turn-off-constraints-temporarily-ms-sql – Turophile Feb 14 '16 at 21:24

0 Answers0