Disabling a Trigger for a Specific SQL Statement or Session

By:   |   Updated: 2008-09-25   |   Comments (10)   |   Related: > Triggers


Problem

I have a trigger created on a table which will fire when either an INSERT, DELETE or UPDATE statements are executed against the table. I want to suppress the trigger from firing for a particular statement while it remains in its normal execution state for any of the other statements. Is there any way this can be done dynamically?

Solution

Disabling a trigger is something that you may need to do at certain times especially when performing admin tasks on a table. The best way to accomplish this is to use the following command to completely disable a trigger.

ALTER TABLE Table_Name DISABLE TRIGGER Trigger_Name

The trigger once disabled will not fire until it is enabled again. To enable the trigger you use the following code:

ALTER TABLE Table_Name ENABLE TRIGGER Trigger_Name

However, if you want to disable the trigger only for a particular statement there is no default mechanism to do this unless you develop your own programmatic approach. Using this kind of approach disables the trigger only for a specific statement while the trigger continues to fire for any of the other statements that hit the server at the same time.

Even though there are different ways to do it, the main logic lies in passing some kind of signal to the trigger that you do not want the trigger to fire.

Using a Temp Table

The simplest way to accomplish this is to create a temporary table before you execute the statement that would fire the trigger. Now the trigger will check for the existence of the temporary table and if the temporary table exists the trigger will return and not execute the code, else it will execute its code as normal.

To see how it works, run the following statements to create a table and a trigger.

USE AdventureWorks; 
GO 
-- creating the table in AdventureWorks database 
IF OBJECT_ID('dbo.Table1') IS NOT NULL 
DROP TABLE dbo.Table1 
GO 
CREATE TABLE dbo.Table1(ID INT) 
GO 
-- Creating a trigger 
CREATE TRIGGER TR_Test ON dbo.Table1 FOR INSERT,UPDATE,DELETE 
AS 
IF OBJECT_ID('tempdb..#Disable') IS NOT NULL RETURN 
PRINT 'Trigger Executed' 
-- Actual code goes here 
-- For simplicity, I did not include any code 
GO

If you do not want the trigger to fire for a statement, let the trigger know by creating the the temporary table in your statement.

CREATE TABLE #Disable(ID INT) 
-- Actual statement 
INSERT dbo.Table1 VALUES(600) 
DROP TABLE #Disable

You will notice that the INSERT statement did not fire the trigger and since the temporary table that was created is local to the session the trigger cannot be bypassed by any of the other sessions.

This works fine, but having to use the Tempdb database to create a temp table and then drop the temp table causes overhead which can be avoided. Using

Context_Info( )

Another way of accomplishing the task is to use the Context Info of the session. Context Info is a variable which belongs to the session. Its value can be changed using SET Context_Info

The trigger will mostly look like this:

USE AdventureWorks; 
GO 
-- creating the table in AdventureWorks database 
IF OBJECT_ID('dbo.Table1') IS NOT NULL 
DROP TABLE dbo.Table1 
GO 
CREATE TABLE dbo.Table1(ID INT) 
GO 
-- Creating a trigger 
CREATE TRIGGER TR_Test ON dbo.Table1 FOR INSERT,UPDATE,DELETE 
AS 
DECLARE @Cinfo VARBINARY(128) 
SELECT @Cinfo = Context_Info() 
IF @Cinfo = 0x55555 
RETURN 
PRINT 'Trigger Executed' 
-- Actual code goes here 
-- For simplicity, I did not include any code 
GO

If you want to prevent the trigger from being executed you can do the following:

SET Context_Info 0x55555 
INSERT dbo.Table1 VALUES(100)

Before issuing the INSERT statement, the context info is set to a value. In the trigger, we are first checking if the value of context info is the same as the value declared. If yes, the trigger will simply return without executing its code, otherwise the trigger will fire.

Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Samuel Vanga Samuel Vanga's bio is coming soon...

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2008-09-25

Comments For This Article




Monday, May 20, 2019 - 10:26:44 AM - smartwombat Back To Top (80132)

I received this answer when I queried why our triggers failed whtn multiple rows were updated.

"This trigger is explicitly implemented to avoid accidental bulk update on customer table. It is highly recommended that we should update one record at a time."

I didn't quite fall off my chair, but I'm astonished that RBAR should be a design decision.

And that started me wondering, how else can single row updates be enforced, as a design decision in a transactional system ?


Saturday, January 21, 2017 - 11:13:07 AM - Kleidi Kumbaro Back To Top (45460)

can the context info be set on a procedure from another database (same instance) and be seen from the current db on the insert trigger? What I want to achieve is the trigger not to run when the inserts are performed as a result of a data transformation on another database from a stored procedure


Wednesday, February 25, 2015 - 10:46:43 AM - Simon Jones Back To Top (36348)

Excellent solution!  Though it is worth noting that the CONTEXT_INFO needs to be set back to 0x000000.... afterwards because the value remains persistent to the SPID which will potentially be recycled if CONNECTION POOLING is in use


Thursday, September 4, 2014 - 11:49:44 AM - Additional Notes Back To Top (34383)
Note that for certain cases, there's also the option to not execute triggers with BCP, BULK INSERT, and even INSERT ... SELECT * FROM OPENROWSET(BULK...) with the WITH(IGNORE_TRIGGERS) table hint. See Controlling Trigger Execution When Bulk Importing Data http://technet.microsoft.com/en-us/library/ms187640%28v=sql.105%29.aspx

Wednesday, July 9, 2014 - 1:44:52 AM - JUAN MIGUEL Back To Top (32597)

Thanks from Spain.

I plan use this mechanism in a stored procedure and there is a note in sql documentation (from Microsoft in Context_info topic) that worry me: 

(..) When you issue SET CONTEXT_INFO in a stored procedure or trigger, unlike in other SET statements, the new value set for the context information persists after the stored procedure or trigger is completed.

Signifies this than new Context_Info value will persist during time? Must I save and restore CONTEXT_info values? Can I Do?

Best Regards,

 

Juanmi


Wednesday, March 28, 2012 - 10:30:35 AM - Helio Back To Top (16674)

Great!!

Thanks from Portugal.


Tuesday, March 20, 2012 - 9:26:05 AM - hrishikesh Back To Top (16533)

Great Article...It really helped me solving my problem.


Thursday, September 9, 2010 - 2:31:13 PM - Kamlesh Back To Top (10141)
Can we alter trigger on a linked server. and also is context_info() value will be available on linked server


Wednesday, October 1, 2008 - 11:39:47 AM - Mstarr Back To Top (1898)

Great tip: didn't know about user-definable Context_Info.
I'm a minimalist, though....
instead of 4 lines:
DECLARE @Cinfo VARBINARY(128
SELECT @Cinfo Context_Info
() 
IF @Cinfo 
0x55555 
RETURN
The following seems to work as well:
IF (Context_info() = 0x55555) return

Cheers


Tuesday, September 30, 2008 - 11:31:26 AM - sandeepbattina Back To Top (1893)

wonderful article,really helpful















get free sql tips
agree to terms