HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMajor

How do I deal with FK constraints when importing data using DTS Import/Export Wizard?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
dtswizardconstraintshowwithusingexportwhenimportingdata

Problem

I am trying to use the SQL Server Import and Export Wizard to copy data from my production db to my dev db but when I do it fails with the error "The INSERT statment conflicted with the FOREIGN KEY constraint" i have over 40 tables with lots of FK constraints, is there some easy way to deal with this without having to write a drop constraint/add constrat script?

Edit:
I just found out that in Web edition of SQL Server, which is what I am running, DTS will not let you save packages.

Solution

I was given this solution over at SQLTeam.com:


Use:

EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'




Then import your data

EXEC sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all'


Using that method I was able to import all the data in with no issues.

Code Snippets

EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
EXEC sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all'

Context

StackExchange Database Administrators Q#43065, answer score: 41

Revisions (0)

No revisions yet.