Query that Returns a Hierarchical List of Trigger Event Types in SQL Server

In SQL Server, you can use the following T-SQL queries to return a hierarchical list of trigger event types.

These examples query the sys.trigger_event_types view, which returns a row for each event or event group on which a trigger can fire.

Example 1 – Return All Rows

This query returns all rows in the sys.trigger_event_types view.

WITH event_types(Type, Type_Name, Parent_Type, Level, Sort) AS   
(  
    SELECT 
      tet.type, 
      tet.type_name, 
      tet.parent_type, 
      1 AS Level,
      CONVERT(nvarchar(255), tet.type_name)
    FROM sys.trigger_event_types tet
    WHERE parent_type IS NULL
    UNION ALL  
    SELECT tet.type,
           CONVERT(nvarchar(64), REPLICATE('|    ' , Level) + tet.type_name),
           tet.parent_type,
           Level + 1,
           CONVERT(nvarchar(255), RTRIM(Sort) + ' > ' + tet.type_name)
    FROM sys.trigger_event_types AS tet
        INNER JOIN event_types AS et
        ON et.type = tet.parent_type
)  
SELECT Type_Name
FROM event_types
ORDER BY Sort;

That query returns 284 rows in my SQL Server 2017 environment and 291 rows in my SQL Server 2019 environment.

Example 2 – Return a Single Event Type

You can modify the previous query so that it returns a specific event type, listed in breadcrumb style.

Here’s an example of returning the CREATE_TABLE event type in breadcrumb style:

WITH event_types(Type, Type_Name, Parent_Type, Level, Sort) AS   
(  
    SELECT 
      tet.type, 
      tet.type_name, 
      tet.parent_type, 
      1 AS Level,
      CONVERT(nvarchar(255), tet.type_name)
    FROM sys.trigger_event_types tet
    WHERE parent_type IS NULL
    UNION ALL  
    SELECT tet.type,
           CONVERT(nvarchar(64), REPLICATE('|    ' , Level) + tet.type_name),
           tet.parent_type,
           Level + 1,
           CONVERT(nvarchar(255), RTRIM(Sort) + ' > ' + tet.type_name)
    FROM sys.trigger_event_types AS tet
        INNER JOIN event_types AS et
        ON et.type = tet.parent_type
)  
SELECT Sort AS [Result]
FROM event_types
WHERE RIGHT(Sort, 12) = 'CREATE_TABLE';

Result:

+--------------------------------------------------------------------------------------------------+
| Result                                                                                           |
|--------------------------------------------------------------------------------------------------|
| DDL_EVENTS > DDL_DATABASE_LEVEL_EVENTS > DDL_TABLE_VIEW_EVENTS > DDL_TABLE_EVENTS > CREATE_TABLE |
+--------------------------------------------------------------------------------------------------+