Share via

what was the process that fired a delete trigger?

DarinH 21 Reputation points
2022-02-02T14:16:27.457+00:00

I have a delete trigger that i need to know if the delete command was done from a stored procedure or a TSQL command. Is there a way to do that? I have tried CONTEXT_INFO() but that seems to always be returning NULL.

I am trying to determine what caused the delete of the row. So if it came from a TSQL then i want it to do a block of code, but if it came from a very specific stored procedure i do NOT want it to do that extra block of code.

Thanks.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


Answer accepted by question author
  1. Erland Sommarskog 133.7K Reputation points MVP Volunteer Moderator
    2022-02-04T13:21:22.273+00:00

    I am trying to determine what caused the delete of the row. So if it came from a TSQL then i want it to do a block of code, but if it came from a very specific stored procedure i do NOT want it to do that extra block of code.

    If you are on SQL 2016 or later, you can do:

    EXEC sp_set_session_context N'SkipThisSection', 1
    -- Command that fires trigger.
    EXEC sp_set_session_context N'SkipThisSection', NULL
    

    In the trigger you do:

    IF isnull(convert(bit, SESSION_CONTEXT(N'SkipThisSection')), 0) = 0
    BEGIN
        --  The special code goes here.
    END
    

    The second call to sp_set_session_context is needed to clear the "variable", else it will remain in the session.

    On any version of SQL Server you can use a temp table as a flag. In the procedure you do:

    CREATE TABLE #SkipThisSection (a int NOT NULL)
    -- Command that fires trigger.
    

    and in the trigger:

    IF object_id('tempdb..#SkipThisSection') IS NULL
    BEGIN
       -- The special code goes here.
    END
    

    `

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 133.7K Reputation points MVP Volunteer Moderator
    2022-02-02T22:46:58.61+00:00

    There is nothing built in to do this, but you need to cater for this yourself. You can indeed use SET CONTEXT_INFO and CONTEXT_INFO() for that purpose, but if you are on SQL 2016 it is better to use sp_sesssion_context and SESSION_CONTEXT(). That is your code needs to set the context and the trigger can read that.

    You could look at @@nestlevel. If it's 2, you are obviously not being called from a stored procedure, but it has to be an ad-hoc batch. But if the T-SQL session is engaging in dynamic SQL @@nestlevel will be 3 or 4. Likewise if the trigger was fired by another trigger.

    What is your original problem?

    1 person found this answer helpful.

  2. ZoeHui-MSFT 41,551 Reputation points
    2022-02-03T08:14:59.133+00:00

    Hi @DarinH ,

    SQL Server Profiler is an interface to create and manage traces and analyze and replay trace results. Events are saved in a trace file that can later be analyzed or used to replay a specific series of steps when diagnosing a problem.

    You may use SQL Server Profiler to monitor the delete command.

    sql-server-profiler

    Or you may use SQL Server Extended Events to monitor query performance.

    using-sql-server-extended-events-to-monitor-query-performance

    Regards,

    Zoe


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.