0

I have created script to copy database schema and all the objects. Next, I use this command, C:> sqlcmd –S main\mssqlserver – i c:\test\script.sql to copy the "source" database to the another machine (for testing purpose). It pops up error, "...The INSERT statement conflict with the CHECK constraint "CK_field1"...". It also pops up errors with the Foreign Key constraints.

I have found this, How can foreign key constraints be temporarily disabled using T-SQL?

showing how to disable foreign key constraints temporarily. My question is do I need to do this on the "source" database BEFORE creating the script? And then after I am done creating the "destination" database, I need to go back to enable the constraints on the "source" db again? Thanks in advance for your help.

Community
  • 1
  • 1
user3754205
  • 27
  • 1
  • 6
  • Yes. I think you understand it correctly. – Sorrel Vesper Mar 02 '15 at 06:19
  • Thank you, Sorrel. I used the command EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all" to disable all constraints, but it looks like it doesn't work because when I enable all constraints again, it pops up this error message, Msg 547, Level 16, State 0, Line 1 The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_table1_table2". The conflict occurred in database "DB1", table "dbo.table2", column 'sessionid'. I think it doesn't work because the constraint between these tables is not affected by the disable constraint command. – user3754205 Mar 04 '15 at 03:13

0 Answers0