Disabling or Enabling All Constraints of a Table or Database | Database Journal

Disabling or Enabling All Constraints of a Table or Database

Written By
Eli Leiba
Eli Leiba
Jul 29, 2002
1 minute read


These procedures are very important to use when you need to load a bulk of data that you know is correct but when you do not need the foreign keys and check constraints to interfere while you are loading the data. This is also the case when you want to change key values and don’t want the constraints to get in your way. The following procedure will disable/enable all constraints for a given parametric table.


— PROCEDURE to disable/enable all constraints on a table



CREATE PROCEDURE sp_ConstraintState 

              @TblName   VARCHAR(128),

           @State BIT = 1

AS

DECLARE @SQLState VARCHAR(500)

IF @State = 0

        BEGIN

             SET @SQLState = 'ALTER TABLE '+ @TblName + ' NOCHECK CONSTRAINT ALL'

     END

ELSE

   BEGIN

                    SET @SQLState = 'ALTER TABLE ' + @TblName + ' CHECK CONSTRAINT ALL'

   END

EXEC (@SQLState)

go

— For example: exec sp_ConstraintState  'products',0 

will disable all constraints on the products table in northwind


— PROCEDURE to disable/enable all constraints in an entire databse (using sp_constraintState)



 Create proc sp_disable_all_const_in_db 

as

  'exec sp_MsForEachTable 'sp_ConstraintState ''?'',0

go

to disbale all constraints in the database 

EXEC sp_disable_all_const_in_db


»


See All Articles by Columnist
Eli Leiba

Eli Leiba

Eli works at Israel Electric Company as a Senior Application DBA in Oracle and MS SQL Server. He also has certifications from Microsoft and BrainBench in Oracle and SQL Server database administration and implementation. Mr. Leiba holds a B.S. in Computer Science since 1991 and has 11 years experience working in the database field. Additionally, Mr. Leiba teaches SQL Server DBA and Development courses at Microsoft CTEC and also serves as a senior database consultant for several Israeli start-up companies.

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.