I’m doing some test data loading with SSIS into our data warehouse and have come across the old chestnut of foreign key constraints. I’m going to be removing/adding all data looking for data issues in my local test environment. Therefore I could care less about referential integrity at this point… I came across this handy little query:
-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NO CHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'TRUNCATE TABLE ?'
GO
-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
The only problem is it is useless if you have foreign key constraints as you have to actually drop them. Is there any way of combining this query with something that will Drop/Recreate all constraints???
Unfortunately no
You have to DELETE tables with foreign keys
Or, script and DROP your constraints first, add tjem back later.
To that end, see this article to “Script out your foreign keys”