Skip to content

Instantly share code, notes, and snippets.

@PRElias
Created January 22, 2019 13:59
Show Gist options
  • Save PRElias/2c655b6f33f10180ebbf6ee4dc0c0f4a to your computer and use it in GitHub Desktop.
Save PRElias/2c655b6f33f10180ebbf6ee4dc0c0f4a to your computer and use it in GitHub Desktop.
-- Get ALTER TABLE Script for all tables to Enable all constraints for database
SELECT distinct 'ALTER TABLE [' + s.name + '].[' + o.name + '] WITH CHECK CHECK CONSTRAINT all'
FROM sys.foreign_keys i
INNER JOIN sys.objects o ON i.parent_object_id = o.OBJECT_ID
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
GO
-- Get ALTER TABLE Script for all tables to Disable all constraints for database
SELECT distinct 'ALTER TABLE [' + s.name + '].[' + o.name + '] NOCHECK CONSTRAINT all'
FROM sys.foreign_keys i
INNER JOIN sys.objects o ON i.parent_object_id = o.OBJECT_ID
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment