Home > DeveloperSection > Forums > What is the best way to check whether a trigger exists in SQL Server?
John Smith

Total Post:101

Posted on    October-16-2012 3:40 AM

 MSSQL Server MSSQL Server 

 1 Reply(s)
 2541  View(s)
Rate this:
I am looking for the most portable method to check for existance of a trigger in MS SQL Server. It needs to work on at least SQL Server 2000, 2005 and preferably 2008.

The information does not appear to be in INFORMATION_SCHEMA, but if it is in there somewhere, I would prefer to use it from there.

I do know of this method:

if exists (
    select * from dbo.sysobjects
    where name = 'MyTrigger'
    and OBJECTPROPERTY(id, 'IsTrigger') = 1


But I am not sure whether it works on all SQL Server versions.

So my questions are:

    Is the above the "best" way?
    Are there any alternative methods?
    What are their pros and cons?

Carl Pieterson
Carl Pieterson

Total Post:14

Posted on    October-16-2012 3:45 AM

There's also the preferred "sys.triggers" catalog view:

select * from sys.triggers where name = 'MyTrigger'

or call the sp_Helptrigger stored proc:

exec sp_helptrigger 'MyTableName'

But other than that, I guess that's about it :-)


Update (for Jakub Januszkiewicz):

If you need to include the schema information, you could also do something like this:

    (list of columns)
FROM sys.triggers tr
INNER JOIN sys.tables t ON tr.parent_id = t.object_id
WHERE t.schema_id = SCHEMA_ID('dbo')   -- or whatever you need

Don't want to miss updates? Please click the below button!

Follow MindStick