-- Disable all table constraintsALTER TABLE MyTable NOCHECK CONSTRAINT ALL
-- Enable all table constraintsALTER TABLE MyTable WITH CHECK CHECK CONSTRAINT ALL
-- Disable single constraint
ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint
-- Enable single constraintALTER TABLE MyTable WITH CHECK CHECK CONSTRAINT MyConstraint
ALTER TABLE [TABLE_NAME] WITH CHECK CHECK CONSTRAINT [ALL|CONSTRAINT_NAME]
启用时注意双检查检查。
ALL表示表中的所有约束。
完成后,如果您需要检查状态,请使用此脚本列出约束状态。将非常有帮助:
SELECT (CASEWHEN OBJECTPROPERTY(CONSTID, 'CNSTISDISABLED') = 0 THEN 'ENABLED'ELSE 'DISABLED'END) AS STATUS,OBJECT_NAME(CONSTID) AS CONSTRAINT_NAME,OBJECT_NAME(FKEYID) AS TABLE_NAME,COL_NAME(FKEYID, FKEY) AS COLUMN_NAME,OBJECT_NAME(RKEYID) AS REFERENCED_TABLE_NAME,COL_NAME(RKEYID, RKEY) AS REFERENCED_COLUMN_NAMEFROM SYSFOREIGNKEYSORDER BY TABLE_NAME, CONSTRAINT_NAME,REFERENCED_TABLE_NAME, KEYNO
----------------------------------------------------------------------------1)/*Author: DenmachDateCreated: 2014-04-23Purpose: Generates SQL statements to DROP, ADD, and CHECK existing constraints for adatabase. Stores scripts in tables on target database for execution. Executesthose stored scripts via independent cursors.DateModified:ModifiedByComments: This will eliminate deletes and the T-log ballooning associated with it.*/
DECLARE @schema_name SYSNAME;DECLARE @table_name SYSNAME;DECLARE @constraint_name SYSNAME;DECLARE @constraint_object_id INT;DECLARE @referenced_object_name SYSNAME;DECLARE @is_disabled BIT;DECLARE @is_not_for_replication BIT;DECLARE @is_not_trusted BIT;DECLARE @delete_referential_action TINYINT;DECLARE @update_referential_action TINYINT;DECLARE @tsql NVARCHAR(4000);DECLARE @tsql2 NVARCHAR(4000);DECLARE @fkCol SYSNAME;DECLARE @pkCol SYSNAME;DECLARE @col1 BIT;DECLARE @action CHAR(6);DECLARE @referenced_schema_name SYSNAME;
--------------------------------Generate scripts to drop all foreign keys in a database --------------------------------
IF OBJECT_ID('dbo.sync_dropFK') IS NOT NULLDROP TABLE sync_dropFK
CREATE TABLE sync_dropFK(ID INT IDENTITY (1,1) NOT NULL, Script NVARCHAR(4000))
DECLARE FKcursor CURSOR FOR
SELECTOBJECT_SCHEMA_NAME(parent_object_id), OBJECT_NAME(parent_object_id), nameFROMsys.foreign_keys WITH (NOLOCK)ORDER BY1,2;
OPEN FKcursor;
FETCH NEXT FROM FKcursor INTO@schema_name, @table_name, @constraint_name
WHILE @@FETCH_STATUS = 0
BEGINSET @tsql = 'ALTER TABLE '+ QUOTENAME(@schema_name)+ '.'+ QUOTENAME(@table_name)+ ' DROP CONSTRAINT '+ QUOTENAME(@constraint_name)+ ';';--PRINT @tsql;INSERT sync_dropFK (Script)VALUES (@tsql)
FETCH NEXT FROM FKcursor INTO@schema_name, @table_name, @constraint_name;
END;
CLOSE FKcursor;
DEALLOCATE FKcursor;
---------------Generate scripts to create all existing foreign keys in a database ------------------------------------------------------------------------------------------------------------------------------------------IF OBJECT_ID('dbo.sync_createFK') IS NOT NULLDROP TABLE sync_createFK
CREATE TABLE sync_createFK(ID INT IDENTITY (1,1) NOT NULL, Script NVARCHAR(4000))
IF OBJECT_ID('dbo.sync_createCHECK') IS NOT NULLDROP TABLE sync_createCHECK
CREATE TABLE sync_createCHECK(ID INT IDENTITY (1,1) NOT NULL, Script NVARCHAR(4000))
DECLARE FKcursor CURSOR FOR
SELECTOBJECT_SCHEMA_NAME(parent_object_id), OBJECT_NAME(parent_object_id), name, OBJECT_NAME(referenced_object_id), OBJECT_ID, is_disabled, is_not_for_replication, is_not_trusted, delete_referential_action, update_referential_action, OBJECT_SCHEMA_NAME(referenced_object_id)
FROMsys.foreign_keys WITH (NOLOCK)
ORDER BY1,2;
OPEN FKcursor;
FETCH NEXT FROM FKcursor INTO@schema_name, @table_name, @constraint_name, @referenced_object_name, @constraint_object_id, @is_disabled, @is_not_for_replication, @is_not_trusted, @delete_referential_action, @update_referential_action, @referenced_schema_name;
WHILE @@FETCH_STATUS = 0
BEGIN
BEGINSET @tsql = 'ALTER TABLE '+ QUOTENAME(@schema_name)+ '.'+ QUOTENAME(@table_name)+ CASE@is_not_trustedWHEN 0 THEN ' WITH CHECK 'ELSE ' WITH NOCHECK 'END+ ' ADD CONSTRAINT '+ QUOTENAME(@constraint_name)+ ' FOREIGN KEY (';
SET @tsql2 = '';
DECLARE ColumnCursor CURSOR FOR
SELECTCOL_NAME(fk.parent_object_id, fkc.parent_column_id), COL_NAME(fk.referenced_object_id, fkc.referenced_column_id)
FROMsys.foreign_keys fk WITH (NOLOCK)INNER JOIN sys.foreign_key_columns fkc WITH (NOLOCK) ON fk.[object_id] = fkc.constraint_object_id
WHEREfkc.constraint_object_id = @constraint_object_id
ORDER BYfkc.constraint_column_id;
OPEN ColumnCursor;
SET @col1 = 1;
FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;
WHILE @@FETCH_STATUS = 0
BEGINIF (@col1 = 1)SET @col1 = 0;ELSEBEGINSET @tsql = @tsql + ',';SET @tsql2 = @tsql2 + ',';END;
SET @tsql = @tsql + QUOTENAME(@fkCol);SET @tsql2 = @tsql2 + QUOTENAME(@pkCol);--PRINT '@tsql = ' + @tsql--PRINT '@tsql2 = ' + @tsql2FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;--PRINT 'FK Column ' + @fkCol--PRINT 'PK Column ' + @pkColEND;
CLOSE ColumnCursor;DEALLOCATE ColumnCursor;
SET @tsql = @tsql + ' ) REFERENCES '+ QUOTENAME(@referenced_schema_name)+ '.'+ QUOTENAME(@referenced_object_name)+ ' ('+ @tsql2 + ')';
SET @tsql = @tsql+ ' ON UPDATE '+CASE @update_referential_actionWHEN 0 THEN 'NO ACTION 'WHEN 1 THEN 'CASCADE 'WHEN 2 THEN 'SET NULL 'ELSE 'SET DEFAULT 'END
+ ' ON DELETE '+CASE @delete_referential_actionWHEN 0 THEN 'NO ACTION 'WHEN 1 THEN 'CASCADE 'WHEN 2 THEN 'SET NULL 'ELSE 'SET DEFAULT 'END
+CASE @is_not_for_replicationWHEN 1 THEN ' NOT FOR REPLICATION 'ELSE ''END+ ';';
END;
-- PRINT @tsqlINSERT sync_createFK(Script)VALUES (@tsql)
-------------------Generate CHECK CONSTRAINT scripts for a database ----------------------------------------------------------------------------------------------------------------------------------------
BEGIN
SET @tsql = 'ALTER TABLE '+ QUOTENAME(@schema_name)+ '.'+ QUOTENAME(@table_name)+CASE @is_disabledWHEN 0 THEN ' CHECK 'ELSE ' NOCHECK 'END+ 'CONSTRAINT '+ QUOTENAME(@constraint_name)+ ';';--PRINT @tsql;INSERT sync_createCHECK(Script)VALUES (@tsql)END;
FETCH NEXT FROM FKcursor INTO@schema_name, @table_name, @constraint_name, @referenced_object_name, @constraint_object_id, @is_disabled, @is_not_for_replication, @is_not_trusted, @delete_referential_action, @update_referential_action, @referenced_schema_name;
END;
CLOSE FKcursor;
DEALLOCATE FKcursor;
--SELECT * FROM sync_DropFK--SELECT * FROM sync_CreateFK--SELECT * FROM sync_CreateCHECK---------------------------------------------------------------------------2.)---------------------------------------------------------------------------------------------------------------------------------------------execute Drop FK Scripts --------------------------------------------------
DECLARE @scriptD NVARCHAR(4000)
DECLARE DropFKCursor CURSOR FORSELECT ScriptFROM sync_dropFK WITH (NOLOCK)
OPEN DropFKCursor
FETCH NEXT FROM DropFKCursorINTO @scriptD
WHILE @@FETCH_STATUS = 0BEGIN--PRINT @scriptDEXEC (@scriptD)FETCH NEXT FROM DropFKCursorINTO @scriptDENDCLOSE DropFKCursorDEALLOCATE DropFKCursor--------------------------------------------------------------------------------3.)
----------------------------------------------------------------------------------------------------------------------------------------------Truncate all tables in the database other than our staging tables --------------------------------------------------------------------------------------------------------------------------------------
EXEC sp_MSforeachtable 'IF OBJECT_ID(''?'') NOT IN(ISNULL(OBJECT_ID(''dbo.sync_createCHECK''),0),ISNULL(OBJECT_ID(''dbo.sync_createFK''),0),ISNULL(OBJECT_ID(''dbo.sync_dropFK''),0))BEGIN TRYTRUNCATE TABLE ?END TRYBEGIN CATCHPRINT ''Truncation failed on''+ ? +''END CATCH;'GO------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------execute Create FK Scripts and CHECK CONSTRAINT Scripts-------------------------------------------tack me at the end of the ETL in a SQL task--------------------------------------------------------------------------------------------------------------------------DECLARE @scriptC NVARCHAR(4000)
DECLARE CreateFKCursor CURSOR FORSELECT ScriptFROM sync_createFK WITH (NOLOCK)
OPEN CreateFKCursor
FETCH NEXT FROM CreateFKCursorINTO @scriptC
WHILE @@FETCH_STATUS = 0BEGIN--PRINT @scriptCEXEC (@scriptC)FETCH NEXT FROM CreateFKCursorINTO @scriptCENDCLOSE CreateFKCursorDEALLOCATE CreateFKCursor-------------------------------------------------------------------------------------------------DECLARE @scriptCh NVARCHAR(4000)
DECLARE CreateCHECKCursor CURSOR FORSELECT ScriptFROM sync_createCHECK WITH (NOLOCK)
OPEN CreateCHECKCursor
FETCH NEXT FROM CreateCHECKCursorINTO @scriptCh
WHILE @@FETCH_STATUS = 0BEGIN--PRINT @scriptChEXEC (@scriptCh)FETCH NEXT FROM CreateCHECKCursorINTO @scriptChENDCLOSE CreateCHECKCursorDEALLOCATE CreateCHECKCursor