SQL Server 2005:T-SQL暂时禁用触发器

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

是否可以为一批命令禁用触发器,然后在该批处理完成后启用它?

我确信我可以放下扳机重新添加,但我想知道是否还有其他方法。


1
2
DISABLE TRIGGER { [ schema_name . ] trigger_name [ ,...n ] | ALL }
ON { object_name | DATABASE | ALL SERVER } [ ; ]

msdn.microsoft.com http:/ / / /图书馆/销售额ms189748(.aspx sql.90)P></

逆followed by the:P></

1
2
ENABLE TRIGGER { [ schema_name . ] trigger_name [ ,...n ] | ALL }
ON { object_name | DATABASE | ALL SERVER } [ ; ]

msdn.microsoft.com http:/ / / /图书馆/销售额ms182706(.aspx sql.90)P></


有时从外部数据库的数据填充an empty or in the源码调试数据库问题need to disable的触发器和约束。to do我使用下面的代码:P></

to disable的约束和触发器:P></

1
2
sp_msforeachtable"ALTER TABLE ? NOCHECK CONSTRAINT all"
sp_msforeachtable"ALTER TABLE ? DISABLE TRIGGER  all"

在约束和触发器:to enableP></

1
2
exec sp_msforeachtable @command1="print '?'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
sp_msforeachtable @command1="print '?'", @command2="ALTER TABLE ? ENABLE TRIGGER  all"

found that some time的在线解决方案针sqlservercentral needed to modify the,but as the original约束的一部分使工件完全did notP></


不管一个人多,它是几乎总是在我这个坏主意。You Will of the混乱与完整的数据库。Do not without the ramifications做considering和检查如果你have them with the DBA。P></

如果你确定的队列随访麦特餐馆to the触发后记得把。和for everyone is the disabled触发第12 inserting deleting,while it from the table or is not just for OFF转过身,我知道你的进程,那么if it must be done做小时,在主动数据库is when the最小二乘(单用户模式和preferably)。P></

如果你给to need to this amount of大进出口日期,然后考虑,does not the insert触发器火大。但然后你after the process have to fix散装insert将弹出任何日期去烧你诚信问题介绍了by the触发器。P></


这里的to extend麦特餐馆given example is an答案,网上的MSDN。P></

1
2
3
4
5
6
USE AdventureWorks;
GO
DISABLE TRIGGER Person.uAddress ON Person.Address;
GO
ENABLE Trigger Person.uAddress ON Person.Address;
GO

另一种方法effectively is to disable the trigger without using an additional disabling恩真的变了,that is into the trigger编入。P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
create trigger [SomeSchema].[SomeTableIsEditableTrigger] ON [SomeSchema].[SomeTable]
for insert, update, delete
as
declare
    @isTableTriggerEnabled bit;

exec usp_IsTableTriggerEnabled -- Have to use USP instead of UFN for access to #temp
    @pTriggerProcedureIdOpt  = @@procid,    
    @poIsTableTriggerEnabled = @isTableTriggerEnabled out;

if (@isTableTriggerEnabled = 0)
    return;

-- Rest of existing trigger
go

我读了一个for the variable type of some锁控制记录表(最好在if公司to the context of the current session context),使用_ info(),或是使用the table name of a存在温度(which is already session扫帚有限公司):P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
create proc [usp_IsTableTriggerEnabled]
    @pTriggerProcedureIdOpt  bigint          = null, -- Either provide this
    @pTableNameOpt           varchar(300)    = null, -- or this
    @poIsTableTriggerEnabled bit             = null out
begin

    set @poIsTableTriggerEnabled = 1; -- default return value (ensure not null)

    -- Allow a particular session to disable all triggers (since local
    -- temp tables are session scope limited).
    --
    if (object_id('tempdb..#Common_DisableTableTriggers') is not null)
    begin
        set @poIsTableTriggerEnabled = 0;
        return;
    end

    -- Resolve table name if given trigger procedure id instead of table name.
    -- Google:"How to get the table name in the trigger definition"
    --
    set @pTableNameOpt = coalesce(
         @pTableNameOpt,
         (select object_schema_name(parent_id) + '.' + object_name(parent_id) as tablename
           from sys.triggers
           where object_id = @pTriggerProcedureIdOpt)
    );

    -- Else decide based on logic involving @pTableNameOpt and possibly current session
end

触发器:在then to disableP></

1
2
3
select 1 as A into #Common_DisableTableTriggers;
-- do work
drop table #Common_DisableTableTriggers; -- or close connection

在potentially downside is that the trigger is专业slowed depending permanently Down on the of accessing of the被变量的复杂性。P></

编辑:这amazingly增参考模式相似,2008年后,塞缪尔的铲。P></


1
2
3
ALTER TABLE table_name DISABLE TRIGGER TRIGGER_NAME
-- Here your SQL query
ALTER TABLE table_name ENABLE TRIGGER TRIGGER_NAME


最佳答案for not the batch programming,but for this question搜索找人在快速和容易的方式来触发temporarily to disable,this can be accomplished在SQL服务器管理工作室。P></

  • 触发器EXPAND on the table the folder
  • 右点击the trigger
  • disable
  • enter image description hereP></

    后续process to enable the same国王。P></