0

I have scripted data from a database and want to import them to another, however, I'm getting a FOREIGN KEY SAME TABLE error. After looking at the data I've noticed that FK the row is expecting is created couple of insert statements below. Is there a way to execute the script without having to manually change the order of insert statements?

NOTE: I am using dacpac publish from sqlproj (SDK)

Thom A
  • 88,727
  • 11
  • 45
  • 75
Branislav B.
  • 509
  • 7
  • 21
  • Can you separate the foreign key constraint definition from the table creation? If so, you can just change the create table not to define the foreign key yet, then the inserts would come and finally, a table alter would run, adding the foreign key constraint. – Lajos Arpad May 29 '23 at 15:53
  • Sample data and expected results would help immensely – Charlieface May 29 '23 at 20:30
  • You can disable FK constraint checks and then enable them at the end of the script: https://learn.microsoft.com/en-us/sql/relational-databases/tables/disable-foreign-key-constraints-with-insert-and-update-statements?view=sql-server-ver16 – Alex May 29 '23 at 23:19
  • thanks, can u add id as an answer so I can mark it ? – Branislav B. Jun 07 '23 at 21:08

1 Answers1

0

Disable the constraint check first

ALTER TABLE dbo.myTable NOCHECK CONSTRAINT FK_myTable_myTable GO

then enable it using

ALTER TABLE dbo.myTable CHECK CONSTRAINT FK_myTable_myTable GO

Branislav B.
  • 509
  • 7
  • 21