SQL Server 2005: T-SQL to temporarily disable a trigger

前端 未结 7 1152
太阳男子
太阳男子 2020-12-08 04:33

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

7条回答
  •  星月不相逢
    2020-12-08 04:56

    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

提交回复
热议问题