SQL Server: Disable All Constraints

To disable all constraints on all tables in a database:

[sourcecode language="sql"]
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
[/sourcecode]

To enable all constraints on all tables in a database:

[sourcecode language="sql"]
exec sp_msforeachtable @command1="print '?'",
@command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
[/sourcecode]
Be Sociable, Share!
This entry was posted in SQL Server. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *