Explain Codes LogoExplain Codes Logo

Need to list all triggers in SQL Server database with table name and table's schema

sql
trigger-engineering
database-security
permissions
Nikita BarsukovbyNikita Barsukov·Jan 19, 2025
TLDR

Quickly fetch SQL Server triggers, tables, and their schemas with below query:

SELECT tr.name AS `Trigger`, tb.name AS `Table`, SCHEMA_NAME(tb.schema_id) AS `Schema` FROM sys.triggers tr INNER JOIN sys.tables tb ON tr.parent_id = tb.object_id ORDER BY `Table`, `Trigger`;

This simple query clearly displays trigger names, the tables they're associated with, and their schemas. Ideal for a fast snapshot of your database triggers.

Extending your trigger knowledge

Unveiling more trigger properties

To get insights on type of operation a trigger is meant for, you leverage an OBJECTPROPERTY which is--no surprise--an object's property.

SELECT tr.name AS `Trigger`, tb.name AS `Table`, SCHEMA_NAME(tb.schema_id) AS `Schema`, OBJECTPROPERTY(tr.object_id, 'ExecIsUpdateTrigger') AS `IsUpdate`, -- Trigger bingo for update? OBJECTPROPERTY(tr.object_id, 'ExecIsDeleteTrigger') AS `IsDelete`, -- Or maybe it's delete? OBJECTPROPERTY(tr.object_id, 'ExecIsInsertTrigger') AS `IsInsert` -- Surely not another insert trigger! FROM sys.triggers tr INNER JOIN sys.tables tb ON tr.parent_id = tb.object_id

Better view on trigger operation facilitates pinpoint troubleshooting and potential boost in performance.

Working with older SQL Server versions

In dealing with the golden oldie--SQL Server 2000--you will be working with sysobjects and possibly sysusers:

SELECT name AS `Trigger`, USER_NAME(uid) AS `Owner`, OBJECT_NAME(parent_obj) AS `Table` FROM sysobjects WHERE xtype = 'TR' -- 'TR' for trigger, not to be confused with 'T-Rex'

GUI lovers: SQL Server Management Studio (SSMS)

For those who adore visual interfaces, SSMS Object Explorer serves as a graphic interface, helping you analyze triggers visually. It's like a painting for database enthusiasts.

Scrutinizing DDL triggers

System-level triggers are critical too

Let's not forget the vigilante of SQL Server world, the almighty DDL triggers. They are crucial in auditing changes to the database schema or server objects:

SELECT name AS `Trigger`, OBJECT_NAME(parent_id) AS `Scope` FROM sys.triggers WHERE parent_class_desc = 'DATABASE' OR parent_class_desc = 'SERVER' -- It's a bird...It's a plane...No, it's a DDL trigger!

DDL triggers aren’t tied to a specific table. Instead, they like to keep their scopes broad--either DATABASE or the biggie, SERVER.

Whose trigger is it?

To fathom the ownership of triggers, use the USER_NAME function to reveal the owner's name:

SELECT name AS `Trigger`, USER_NAME(uid) AS `Owner` FROM sysobjects WHERE xtype = 'TR' -- 'TR' for trigger; 'T-Rex' would be too awesome...

Ownership helps maintain database security standards and manage permissions.

Finding solutions to common issues

When queries throw curveballs

Stumped by a non-executing query? Could be due to a version discrepancy or dysfunctional system tables. Always cross-check if you're scripting the right code for your SQL Server version.

Managing a sea of triggers

In environments churning with data activity, where galaxy of triggers exist, limit results or batch queries comes handy to avoid straining system resources or SSMS.

Permissions and security revolving around triggers

Access to trigger metadata should be carefully regulated. Overseeing permissions ensures robust security and uncompromised data integrity.