Question

I have a database trigger that I use to prevent me from creating certain procedures in user databases.

It appears in sys.triggers, with an object_id, but I can't use the object_id function to find it.

SELECT OBJECT_ID(t.name, t.type) AS object_id, *
FROM   sys.triggers AS t;

NUTS

Likewise, I can find it in sys.dm_exec_trigger_stats. I can't get object_name to resolve, but object_definition does.

SELECT OBJECT_NAME(dets.object_id, dets.database_id) AS object_name,
       OBJECT_DEFINITION(dets.object_id) AS object_definition,
       *
FROM   sys.dm_exec_trigger_stats AS dets;

NUTS

Is there a function that will accept a database level trigger's object id, and return its name?

Was it helpful?

Solution

Database- and server-level triggers are not scoped as "objects" per se (this is why you cannot create them under a schema, and why they don't show up in sys.objects).

You can see that these objects have certain restrictions on them, for example in the OBJECTPROPERTY() docs:

This function cannot be used for objects that are not schema-scoped, such as data definition language (DDL) triggers and event notifications.

And similarly in the OBJECTPROPERTYEX() docs:

OBJECTPROPERTYEX cannot be used for objects that are not schema-scoped, such as data definition language (DDL) triggers and event notifications.

The OBJECT_ID() docs are a little more explicit:

Objects that are not schema-scoped, such as DDL triggers, cannot be queried by using OBJECT_ID. For objects that are not found in the sys.objects catalog view, obtain the object identification numbers by querying the appropriate catalog view. For example, to return the object identification number of a DDL trigger, use SELECT OBJECT_ID FROM sys.triggers WHERE name = 'DatabaseTriggerLog'.

The OBJECT_NAME() docs are less explicit, but they mention the same restriction implicitly (emphasis mine):

Returns the database object name for schema-scoped objects.


For the first query, not sure why you need to get the name via the function, since the name column in sys.triggers already gives you that answer. For the second query, you could just join to sys.triggers:

SELECT tr.*, ts.*
FROM sys.dm_exec_trigger_stats AS ts
LEFT OUTER JOIN sys.triggers AS tr
ON ts.[object_id] = tr.[object_id];

You could create your own function, of course, but I don't know of any built-in functions that do this correlation for you (and I recommend staying away from built-in metadata functions generally anyway).

DDL triggers are kind of a special animal. So if you're worried about also having to join to sys.procedures, sys.views, etc., don't.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top