Temporarily disabling foreign key constraints in SQL Server

Posted by Renso on Geeks with Blogs See other posts from Geeks with Blogs or by Renso
Published on Mon, 24 Jun 2013 01:50:05 GMT Indexed on 2013/06/24 16:23 UTC
Read the original article Hit count: 501

Filed under:

Originally posted on: http://geekswithblogs.net/renso/archive/2013/06/24/temporarily-disabling-foreign-key-constraints-in-sql-server.aspx

Goal: Is to temporarily disable all foreign key constraint and later enable the Constraint again?

Solutions

-- Disable all the constraint in database
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

-- Enable all the constraint in database
EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"

© Geeks with Blogs or respective owner

Related posts about sql