SQL Server Trigger Call Count

insertsql servertrigger

Is there are way to find how many times trigger is called in a particular session? Our ETL application is inserting records like each row instead of statement (ie) If 100 records are to be inserted I think ETL issuing 100 insert statements instead of inserting in a single batch. So trigger is also called 100 times. I just want to confirm that trigger is called 100 times.

I have a idea to alter the trigger to have counter and store it in a table to find the number of trigger calls.

Is there a native way to find this?

Best Answer

You can set up an Extended Event trace and capture event type 'sp_statement_completed'. Filter the trace by object_type='trigger'. You can also pass your trigger name to narrow down further. Following definition will give you a starting point. Adjust file name, location, size and max_file_size based on your need.

CREATE event session [CaptureTriggerExecution] ON server ADD event sqlserver.sp_statement_completed( WHERE (
  [package0].[equal_uint64]([object_type],(21076)) 
  AND 
  [object_name]=N'''My Trigger''' 
) 
) ADD target package0.event_file(SET filename=N'CaptureTriggerExecution', 
  max_file_size=(10)) WITH (max_memory=4096 kb, 
event_retention_mode=allow_single_event_loss, 
max_dispatch_latency=30 seconds, 
max_event_size=0 kb, 
memory_partition_mode=none, 
track_causality=ON, 
startup_state=OFF)
GO