SQL Server: Disable All Constraints - YAGNI



SQL Server: Disable All Constraints

May 3, 2011 © Kramii (filed under SQL Server)

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!

Share Freely

You are welcome to share this with your friends.
Be Sociable, Share!
Leave a comment »


Leave a Reply

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