Disabling and Enabling All Foreign Keys
CREATE PROCEDURE pr_Disable_Triggers_v2
@disable BIT = 1
AS
DECLARE @sql VARCHAR(500)
, @tableName VARCHAR(128)
, @tableSchema VARCHAR(128)
-- List of all tables
DECLARE triggerCursor CURSOR FOR
SELECT t.TABLE_NAME AS TableName
, t.TABLE_SCHEMA AS TableSchema
FROM INFORMATION_SCHEMA.TABLES t
ORDER BY t.TABLE_NAME, t.TABLE_SCHEMA
OPEN triggerCursor
FETCH NEXT FROM triggerCursor INTO @tableName, @tableSchema
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
SET @sql = 'ALTER TABLE ' + @tableSchema + '.[' + @tableName + '] '
IF @disable = 1
SET @sql = @sql + ' DISABLE TRIGGER ALL'
ELSE
SET @sql = @sql + ' ENABLE TRIGGER ALL'
PRINT 'Executing Statement - ' + @sql
EXECUTE ( @sql )
FETCH NEXT FROM triggerCursor INTO @tableName, @tableSchema
END
CLOSE triggerCursor
DEALLOCATE triggerCursor
First, the foreignKeyCursor cursor is declared as the SELECT statement
that gathers the list of foreign keys and their table names. Next, the
cursor is opened and the initial FETCH statement is executed. This
FETCH statement will read the first row's data into the local
variables @foreignKeyName and @tableName. When looping through a
cursor, you can check the @@FETCH_STATUS for a value of 0, which
indicates that the fetch was successful. This means the loop will
continue to move forward so it can get each successive foreign key
from the rowset. @@FETCH_STATUS is available to all cursors on the
connection. So if you are looping through multiple cursors, it is
important to check the value of @@FETCH_STATUS in the statement
immediately following the FETCH statement. @@FETCH_STATUS will reflect
the status for the most recent FETCH operation on the connection.
Valid values for @@FETCH_STATUS are:
0 = FETCH was successful
-1 = FETCH was unsuccessful
-2 = the row that was fetched is missing
Inside the loop, the code builds the ALTER TABLE command differently
depending on whether the intention is to disable or enable the foreign
key constraint (using the CHECK or NOCHECK keyword). The statement is
then printed as a message so its progress can be observed and then the
statement is executed. Finally, when all rows have been iterated
through, the stored procedure closes and deallocates the cursor.
see Disabling Constraints and Triggers from MSDN Magazine