禁用单个插入语句上的触发器

时间:2012-03-28 08:46:20

标签: sql sql-server sql-server-2008

我有一个AFTER INSERT触发器,适用于单行插入,但使用插入>的INSERT SELECT语句表现不佳10,000行/

是否可以在语句级别禁用触发器,例如对于单个INSERT SELECT语句?

3 个答案:

答案 0 :(得分:2)

DISABLE TRIGGER dbo.MyTrigger ON dbo.MyTable

EXEC MyGeographyStoredProcedure

ENABLE TRIGGER dbo.MyTrigger ON dbo.MyTable

DISABLE TRIGGER dbo.MyTrigger ON dbo.MyTable

INSERT LudicrousNumberofRows FROM BigDataset

ENABLE TRIGGER dbo.MyTrigger ON dbo.MyTable

答案 1 :(得分:1)

当您禁用触发器时,它适用于所有人,而不仅仅是您的连接。那么为什么要尝试禁用它并导致其他插入的潜在问题?

在触发器的开头做:

IF EXISTS (SELECT top 2 COUNT(1) FROM INSERTED HAVING COUNT(1)>1)
BEGIN
    --possibly insert into some other "work needs to be done table"
    --where you have a running job pull this info and work on it
    --so this trigger doesn't need to do it, you just delay the work
    --and let the job do it.
    RETURN --exit the trigger without doing any of the "slow processing"
END

答案 2 :(得分:0)

另一种选择:

ALTER TABLE tablename DISABLE TRIGGER ALL 
-- do work here 
ALTER TABLE tablename ENABLE TRIGGER ALL

如果您只有一个触发器或禁用表上所有触发器的内容,请使用它:)