Pages

Saturday, June 1, 2013

How to disable all constraints and all triggers?

Execute the following Microsoft SQL Server T-SQL script to demonstrate the disabling of all constraints and all triggers in the AdventureWorks2008 sample database. 
USE AdventureWorks2008;

DECLARE @SQLCommand VARCHAR(MAX)
SET @SQLCommand = ''
SELECT @SQLCommand = ISNULL(@SQLCommand,'') + 'ALTER TABLE ' +
QUOTENAME(s.name) + '.' + QUOTENAME(o.name) +
' NOCHECK CONSTRAINT ALL;' + CHAR(13)
FROM sys.objects o
JOIN sys.schemas s
on o.schema_id = s.schema_id
WHERE type = 'U'
PRINT @SQLCommand
EXEC(@SQLCommand)

SET @SQLCommand = ''
SELECT @SQLCommand = ISNULL(@SQLCommand,'') + 'ALTER TABLE ' +
QUOTENAME(s.name) + '.' + QUOTENAME(o.name) +
' DISABLE TRIGGER ALL;' + CHAR(13)
FROM sys.objects o
JOIN sys.schemas s
on o.schema_id = s.schema_id
WHERE type = 'U'
PRINT @SQLCommand
EXEC(@SQLCommand)

No comments:

Post a Comment