Wednesday, July 13, 2011

Query Trigger

I had a need recently to programattically query the triggers for a set of tables in my database. Here's the code that I used:

 SELECT DISTINCT
Tables.Name TableName
, Triggers.name TriggerName
, Triggers.crdate TriggerCreatedDate
, [Type] = CASE WHEN Triggers.xtype = 'TR' THEN 'SQL Trigger'
ELSE 'CLR Trigger'
END
, [Disabled] = OBJECTPROPERTY(OBJECT_ID(Triggers.name), 'ExecIsTriggerDisabled')
FROM
sysobjects Triggers
INNER JOIN sysobjects Tables
ON Triggers.parent_obj = Tables.id
AND Tables.xtype = 'U'
AND Tables.name = 'eperson'
LEFT JOIN syscomments Comments
ON Triggers.id = Comments.id
WHERE
Triggers.xtype IN ( 'TR', 'TA' )
ORDER BY
Tables.Name
, Triggers.name

Updated the above query to handle CLR triggers and to show if the trigger is disabled.

No comments: