Skip to content

Instantly share code, notes, and snippets.

@LetsGoRafting
Created February 13, 2024 21:53
Show Gist options
  • Save LetsGoRafting/db8c41d5ce49fb50899812958e735930 to your computer and use it in GitHub Desktop.
Save LetsGoRafting/db8c41d5ce49fb50899812958e735930 to your computer and use it in GitHub Desktop.
Disable and enable all foreign keys when refreshing tables from DEV, UAT etc
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
select 'delete from ' + name
from sys.objects
where type = 'U'
and name like 'blah-di-blah%'
order by name
-- SSIS that shizzle in there and flip them on again
EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment