• 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.