January 19, 2009 at 5:44 pm
Is there any way to enable and disable triggers during transaction. ?
January 19, 2009 at 6:09 pm
Yes... there is. Please see "Enable Trigger" and "Disable Trigger" in Books Online.
Also, I'd be very, very wary of doing such a thing. The tirgger(s) were put on the table for a reason and disabling them may violate that reason. In practice, it's rarely a good thing to do.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 19, 2009 at 11:51 pm
I agree with Jeff. The only time you may want to disable a trigger is when you are doing a mass load/update/delete that does not have to be covered by the purpose of a trigger (auditing, alerting, etc...).
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
January 21, 2009 at 7:03 am
To disable / enable selective triggers...
ALTER TABLE tableName DISABLE TRIGGER triggername
ALTER TABLE tableName ENABLE TRIGGER triggername
To disable / enable all triggers...
ALTER TABLE tableName DISABLE TRIGGER ALL
ALTER TABLE tableName ENABLE TRIGGER ALL
Use this with caution and ensure proper handling as a transaction might have disabled but an error in the script might fail to enable it back.
Happy SQLing...
January 21, 2009 at 6:44 pm
amartha_dutta (1/21/2009)
To disable / enable selective triggers...ALTER TABLE tableName DISABLE TRIGGER triggername
ALTER TABLE tableName ENABLE TRIGGER triggername
To disable / enable all triggers...
ALTER TABLE tableName DISABLE TRIGGER ALL
ALTER TABLE tableName ENABLE TRIGGER ALL
Use this with caution and ensure proper handling as a transaction might have disabled but an error in the script might fail to enable it back.
Happy SQLing...
Heh... and the OP actually misses the opportunity to use Books Online for the first time in their life. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
January 22, 2009 at 11:58 pm
IN SQL-2000
To disable / enable selective triggers...
ALTER TABLE tableName DISABLE TRIGGER triggername
ALTER TABLE tableName ENABLE TRIGGER triggername
To disable / enable all triggers...
ALTER TABLE tableName DISABLE TRIGGER ALL
ALTER TABLE tableName ENABLE TRIGGER ALL
*******************
IN SQL-2005
To disable / enable selective triggers...
DISABLE TRIGGER triggername ON tableName
ENABLE TRIGGER triggername ON tableName
To disable / enable all triggers...
DISABLE TRIGGER ALL ON tableName [optional:All server]
ENABLE TRIGGER ALL ON tableName [optional:All server]
January 23, 2009 at 6:30 am
I am able to do it by some of other posts in the portal. Thanks for all your info. I am actually working mas data archive and restore stuff. During that time if i enable triggers it leads to lot of other problems. So disabling trigger is must i feel.
January 23, 2009 at 10:01 am
anbillava (1/23/2009)
I am able to do it by some of other posts in the portal. Thanks for all your info. I am actually working mas data archive and restore stuff. During that time if i enable triggers it leads to lot of other problems. So disabling trigger is must i feel.
And, if someone happens to try adding rows during your archive process, BOOM! Bad data because the trigger isn't active.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 20, 2012 at 4:03 am
What utter nonsense
altering a table within a transaction requires an exclusive lock on the table
so nobody can change the table in any way once the trigger is disabled (DDL or DML)
that lock will persist until the end of the transaction
This is basic database locking - if you don't know how locks work - you don't know how transactions work - and you don't know how databases work
This can be proved in 10 seconds by opening two query windows in SSMS and just typing:
[query1]
begin tran
alter table dbo.Container disable trigger trContainer
(execute)
[query2]
select * from dbo.Container
(execute)
-- note the lock wait ...
[query1]
rollback tran
(execute)
[query2]
oh look ... lock finished waiting results return
(execute)
March 20, 2012 at 6:18 am
Jizzy Wig (3/20/2012)
What utter nonsense
Thanks for the example and no problem with disagreement but you really don't need to be that way.
Yes, if you have an explicit transaction or your settings are set to such a thing but a lot of folks forget about things like setting an explicit transaction when they disable triggers. You really don't need to be such a snot about it.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 20, 2012 at 6:30 am
What utter nonsense
Thats no way to speak to such a helpful chap as Jeff 😛
==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
March 21, 2012 at 6:05 am
I want to make sure people realise that enabling and disabling a trigger (usually as part of an archiving procedure) is completely normal and safe.
There is alot of disinformation about this.
There is even a totally ludicrous approach using set context_info by some muppet on stackoverflow
This post was #1 on google when I searched ... and it is not helping anybody.
(Your points are valid if people aren't using transactions properly)
March 21, 2012 at 7:20 am
Jizzy Wig (3/21/2012)
I want to make sure people realise that enabling and disabling a trigger (usually as part of an archiving procedure) is completely normal and safe.There is alot of disinformation about this.
There is even a totally ludicrous approach using set context_info by some muppet on stackoverflow
This post was #1 on google when I searched ... and it is not helping anybody.
(Your points are valid if people aren't using transactions properly)
It may be safe but I don't consider it to be "normal". The trigger is there for a reason and there really needs to be a very good reason to override it IMHO.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 8, 2015 at 1:17 pm
Interesting discussion, we have come across this recently and are looking for a solution.
Might just have to try this, though not too certain about the rollback for the disable transaction, would this have the effect of rolling back all changes within that same transaction? Or can you nest transactions and have the inner ones totally autonomous to the wrapping transaction?
________________________________________________
We passed upon the stair - and I was that man who sold the worldJune 8, 2015 at 1:33 pm
And if I remember correctly when I had a need to disable and enable triggers in a closed process it had to be done using dynamic SQL as the disable and enable of the triggers had to the first (only?) statement(s) in the batch. Just as a note, this was also as part of an application upgrade so NO USERS where able to access the system while this process ran.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply