Home > Technical > Disable and Enable Referential Integrity

Disable and Enable Referential Integrity

The below T-SQL allows you to disable all constraints, run any database cleanup script you may need to and then re-enable the constraints.

– disable referential integrity for all tables in the database
EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
GO

– enable referential integrity again for all tables in the database
EXEC sp_MSForEachTable ‘ALTER TABLE ? CHECK CONSTRAINT ALL’
GO

Categories: Technical Tags:
  1. No comments yet.
  1. No trackbacks yet.