A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
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
`