Temporarily disabling foreign key constraints in SQL Server

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"

Print | posted @ Monday, June 24, 2013 8:50 AM

Comments on this entry:

Gravatar # http://www.web-station.it/
by realizzazione siti web at 12/29/2013 11:15 AM

Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.
realizzazione siti web http://www.web-station.it/
Gravatar # re: Temporarily disabling foreign key constraints in SQL Server
by Anvesh at 2/8/2016 5:31 AM

Nice Article!
It really helps to all people of database community.
I have also prepared one small demo to enable and disable foreign key and check constraint in SQL Server.
You can visit my article here:
http://www.dbrnd.com/2015/09/enable-disable-foreign-key-and-check-constraint-in-sql-server/
Post A Comment
Title:
Name:
Email:
Comment:
Verification: