Is it possible to disable a trigger for a batch of commands and then enable it when the batch is done?
I\'m sure I could drop the trigger and re-add it but I was won
Sometimes to populate an empty database from external data source or debug a problem in the database I need to disable ALL triggers and constraints. To do so I use the following code:
To disable all constraints and triggers:
sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
sp_msforeachtable "ALTER TABLE ? DISABLE TRIGGER all"
To enable all constraints and triggers:
exec sp_msforeachtable @command1="print '?'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
sp_msforeachtable @command1="print '?'", @command2="ALTER TABLE ? ENABLE TRIGGER all"
I found that solution some time ago on SQLServerCentral, but needed to modify the enable constraints part as the original one did not work fully