SQLServerCentral.com / SQL Server 2012 / SQL Server 2012 - T-SQL / Disable only one trigger action / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comTue, 31 Mar 2015 12:55:11 GMT20RE: Disable only one trigger actionhttp://www.sqlservercentral.com/Forums/Topic1567907-3077-1.aspxThank 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).Tue, 06 May 2014 23:51:55 GMTpgmoschettiRE: Disable only one trigger actionhttp://www.sqlservercentral.com/Forums/Topic1567907-3077-1.aspxI 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.Tue, 06 May 2014 14:38:02 GMTScottPletcherRE: Disable only one trigger actionhttp://www.sqlservercentral.com/Forums/Topic1567907-3077-1.aspx[quote][b]Sean Lange (5/6/2014)[/b][hr][quote][b]pgmoschetti (5/6/2014)[/b][hr]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[/quote]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.[/quote]+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:[code="sql"]/* 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;[/code]Also if you are using a application user you could use SYSTEM_USER instead of APP_NAME() to "skip" the delete logic.Tue, 06 May 2014 10:27:50 GMT Jack CorbettRE: Disable only one trigger actionhttp://www.sqlservercentral.com/Forums/Topic1567907-3077-1.aspx[quote][b]pgmoschetti (5/6/2014)[/b][hr]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[/quote]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.Tue, 06 May 2014 08:40:18 GMTSean LangeRE: Disable only one trigger actionhttp://www.sqlservercentral.com/Forums/Topic1567907-3077-1.aspxYou 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, regardsTue, 06 May 2014 08:25:31 GMTpgmoschettiRE: Disable only one trigger actionhttp://www.sqlservercentral.com/Forums/Topic1567907-3077-1.aspx[quote][b]pgmoschetti (5/6/2014)[/b][hr]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[/quote]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.Tue, 06 May 2014 07:40:19 GMTSean LangeDisable only one trigger actionhttp://www.sqlservercentral.com/Forums/Topic1567907-3077-1.aspxI 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 advanceTue, 06 May 2014 06:42:41 GMTpgmoschetti