Can someone double-check my trimmed-down example? When the Documents table is updated, I want it update the EntryDate on the Queue table. However, I don’t want the trigger on the Queue table to fire for THIS PROCESS ONLY. Meaning, if some other process updates the EntryDate on the Queue table while this process would be running, I WOULD want the trigger on the Queue table to fire for that particular transaction. I’m not sure if I need to do any kind of lockout for the below code to ensure no other process gets its toes stepped on. Thanks!
create trigger [dbo].[Documents_trigUpdate] on [dbo].[Documents]
for update
as
begin transaction
alter table [Queue] disable trigger Queue_trigUpdate
update [Queue] set EntryDate = getdate()
alter table [Queue] enable trigger Queue_trigUpdate
commit transaction
go
You’ll want to avoid disabling then re-enabling the trigger, because that will affect all processes and not just the current process. if you put some type of lock, you’ll serialize your code which could become a bottleneck in your system.
An alternative would be to use the
Context_infoof the current connection, and change the trigger on theQueueto not execute if a specific context has been set.This technique is covered in this article, and an example is provided below:
To prevent the trigger from being executed you can do the following: