Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Disable and enable trigger Expand / Collapse
Author
Message
Posted Monday, January 19, 2009 5:44 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 07, 2012 1:29 AM
Points: 46, Visits: 91
Is there any way to enable and disable triggers during transaction. ?
Post #639577
Posted Monday, January 19, 2009 6:09 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:41 PM
Points: 35,951, Visits: 30,234
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #639581
Posted Monday, January 19, 2009 11:51 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:24 AM
Points: 10,907, Visits: 12,540
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

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

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
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #639684
Posted Wednesday, January 21, 2009 7:03 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 01, 2012 12:43 AM
Points: 63, Visits: 110
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...
Post #640677
Posted Wednesday, January 21, 2009 6:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:41 PM
Points: 35,951, Visits: 30,234
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #641303
Posted Thursday, January 22, 2009 11:58 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, August 06, 2013 1:32 AM
Points: 282, Visits: 108
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]

Post #642291
Posted Friday, January 23, 2009 6:30 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 07, 2012 1:29 AM
Points: 46, Visits: 91
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.
Post #642413
Posted Friday, January 23, 2009 10:01 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:41 PM
Points: 35,951, Visits: 30,234
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #642623
Posted Tuesday, March 20, 2012 4:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, July 14, 2012 2:59 AM
Points: 9, Visits: 41
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)
Post #1269424
Posted Tuesday, March 20, 2012 6:18 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:41 PM
Points: 35,951, Visits: 30,234
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1269480
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse