You can use T-SQL to return a list of trigger events in a database in SQL Server.
You can do this using sys.trigger_events
. This system catalog view returns a row for each event or event group on which a trigger can fire.
Example 1 – Basic Usage
Here’s an example to demonstrate.
USE Test; SELECT object_id, OBJECT_NAME(object_id) AS [Trigger Name], type_desc FROM sys.trigger_events;
Result:
+-------------+-------------------+-------------+ | object_id | Trigger Name | type_desc | |-------------+-------------------+-------------| | 286624064 | t1_insert_trigger | INSERT | | 2034106287 | t6ins | INSERT | +-------------+-------------------+-------------+
In this case I’m using the OBJECT_NAME()
function to get each trigger’s name from its object_id
.
Example 2 – Narrow it to a Specific Trigger
You can also use OBJECT_NAME()
to filter the results to a specific trigger name using a WHERE
clause.
SELECT object_id, OBJECT_NAME(object_id) AS [Trigger Name], type_desc FROM sys.trigger_events WHERE OBJECT_NAME(object_id) = 't1_insert_trigger';
Result:
+-------------+-------------------+-------------+ | object_id | Trigger Name | type_desc | |-------------+-------------------+-------------| | 286624064 | t1_insert_trigger | INSERT | +-------------+-------------------+-------------+
Example 3 – Return All Columns
This example returns all columns from this view.
SELECT * FROM sys.trigger_events WHERE OBJECT_NAME(object_id) = 't1_insert_trigger';
Result (using vertical output):
object_id | 286624064 type | 1 type_desc | INSERT is_first | 0 is_last | 0 event_group_type | NULL event_group_type_desc | NULL is_trigger_event | 1
In this example I presented the results using vertical output so that you aren’t forced scroll horizontally.
Getting All Trigger Event Types
You can query the sys.trigger_event_types catalog view to get a list of all trigger event types. These are the events or event groups on which a trigger can fire.
You can also run a query that lists them hierarchically.