Disable/Enable all Foreign keys on a database

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 425
sp_MSforeachtable is a undocumented system stored procedure, which will loop through all tables in a database.. So the below code is useful if you want to disable/enable all Foreign Keys at a time..

-- Disable all Constraints
exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'

--Enable all Constraints
exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'

Comments or Responses

Login to post response