SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Disable only one trigger action


Disable only one trigger action

Author
Message
pgmoschetti
pgmoschetti
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 93
I have a trigger that executes AFTER INSERT, UPDATE, DELETE. Is there a way to disable and then reenable only the "AFTER DELETE", letting AFTER INSERT, UPDATE act normally?
Thank you in advance
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63174 Visits: 17961
pgmoschetti (5/6/2014)
I have a trigger that executes AFTER INSERT, UPDATE, DELETE. Is there a way to disable and then reenable only the "AFTER DELETE", letting AFTER INSERT, UPDATE act normally?
Thank you in advance


You can't actually disable it but you could ALTER the trigger to not include DELETE and then alter it again after you are done doing what I assume must be some bulk data deletion.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
pgmoschetti
pgmoschetti
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 93
You are right, that's a good solution. I've only an additional problem, I must think of: as you said, it is a bulk delete. This bulk delete will be run by a .NET client where the customer has a list of all tables in the database, can select any of them, select a couple of dates and then issue that bulk delete on all the selected tables. Each table has its own trigger and on each table I shall disable that "AFTER DELETE".
I will think on it, taking your suggestion as my starting point,
Thanks, regards
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63174 Visits: 17961
pgmoschetti (5/6/2014)
You are right, that's a good solution. I've only an additional problem, I must think of: as you said, it is a bulk delete. This bulk delete will be run by a .NET client where the customer has a list of all tables in the database, can select any of them, select a couple of dates and then issue that bulk delete on all the selected tables. Each table has its own trigger and on each table I shall disable that "AFTER DELETE".
I will think on it, taking your suggestion as my starting point,
Thanks, regards


If this is something is going to happen frequently I would consider splitting your triggers into two. One for Insert, Update and the other for Delete. Then you can just modify your .NET code to first disable the delete trigger, fire your deletes, enable the delete trigger again. I am not a huge fan of triggers in general but if I do use them I like to keep the logic separate for the different types of actions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jack Corbett
  Jack Corbett
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45037 Visits: 14925
Sean Lange (5/6/2014)
pgmoschetti (5/6/2014)
You are right, that's a good solution. I've only an additional problem, I must think of: as you said, it is a bulk delete. This bulk delete will be run by a .NET client where the customer has a list of all tables in the database, can select any of them, select a couple of dates and then issue that bulk delete on all the selected tables. Each table has its own trigger and on each table I shall disable that "AFTER DELETE".
I will think on it, taking your suggestion as my starting point,
Thanks, regards


If this is something is going to happen frequently I would consider splitting your triggers into two. One for Insert, Update and the other for Delete. Then you can just modify your .NET code to first disable the delete trigger, fire your deletes, enable the delete trigger again. I am not a huge fan of triggers in general but if I do use them I like to keep the logic separate for the different types of actions.


+1 for separating out the triggers based on action.

Another option is to use the APP_NAME() function to skip the delete functionality of your trigger if you are setting the application name attribute of the connection string for the application. This isn't full proof as someone could spoof the application name in the connection string from another app. The code would be something like this:


/* identify deletes */
IF EXISTS(SELECT 1 FROM inserted AS I JOIN deleted D ON I.pk = D.pk)
BEGIN;
IF APP_NAME() = 'BULK DELETE APPLICATION'
BEGIN;
/* do nothing */
RETURN;
END;
ELSE
BEGIN;
/* ON DELETE ACTION */
END;
END;



Also if you are using a application user you could use SYSTEM_USER instead of APP_NAME() to "skip" the delete logic.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
ScottPletcher
ScottPletcher
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19801 Visits: 7412
I use CONTEXT_INFO() to control triggers in those cases. If you disable the trigger, of course other deletes also won't process the trigger, when you might have wanted them to. But CONTEXT_INF() is unique to the session or batch.

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
pgmoschetti
pgmoschetti
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 93
Thank you all for your interesting suggestions. Maybe the CONTEXT_INFO solution is the best in my case: separate logic for AFTER DELETE is good, but in my case the logic is just the same for all conditions (populating a "bridge" table with changes); app name is interesting, but the same app has a "normal" part which needs triggers and a "maintenance" part with bulk deletes which should disable only the AFTER DELETE (I could change the app name).
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search