What is the most portable way to check whether a trigger exists in SQL Server?

后端 未结 9 1762
醉酒成梦
醉酒成梦 2020-12-14 05:50

I\'m looking for the most portable method to check for existence of a trigger in MS SQL Server. It needs to work on at least SQL Server 2000, 2005 and prefe

相关标签:
9条回答
  • 2020-12-14 06:06

    If you're trying to find a server scoped DDL Trigger on SQL Server 2014, you should try sys.server_triggers.

    IF EXISTS (SELECT * FROM sys.server_triggers WHERE name = 'your trigger name')
    BEGIN
        {do whatever you want here}
    END
    

    If I told tou anything incorrect, please let me know.

    Edit: I didn't check for this dm on another versions of SQL Server.

    0 讨论(0)
  • 2020-12-14 06:07

    Tested and doesn't work on SQL Server 2000:

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

    Tested and works ok on SQL Server 2000 and SQL Server 2005:

    select * from dbo.sysobjects
    where name = 'MyTrigger' and OBJECTPROPERTY(id, 'IsTrigger')
    
    0 讨论(0)
  • 2020-12-14 06:08

    Assuming it is a DML trigger:

    IF OBJECT_ID('your_trigger', 'TR') IS NOT NULL
    BEGIN
        PRINT 'Trigger exists'
    END
    ELSE
    BEGIN
        PRINT 'Trigger does not exist'
    END
    

    For other types of objects (tables, views, keys, whatever...), see: http://msdn.microsoft.com/en-us/library/ms190324.aspx under 'type'.

    0 讨论(0)
  • 2020-12-14 06:10

    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 :-)

    Marc

    Update (for Jakub Januszkiewicz):

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

    SELECT
        (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
    
    0 讨论(0)
  • 2020-12-14 06:12

    In addition to the excellent answer by marc_s:

    if the existence check is intended prior to dropping or modifying the trigger in some way, use a direct TSQL try/Catch bock, as the fastest means.

    For instance:

    BEGIN TRY
        DROP TRIGGER MyTableAfterUpdate;
    END TRY
    BEGIN CATCH
        SELECT ERROR_NUMBER() AS erno WHERE erno = 3701; -- may differ in SQL Server < 2005
    END CATCH;
    

    The Error Message will be

    Cannot drop the trigger 'MyTableAfterUpdate', because it does not exist or you do not have permission.
    

    Then simply check if the Executed Result returned rows or not, which is easy in direct sql as well as the programmatic APIs (C#,...).

    0 讨论(0)
  • 2020-12-14 06:15

    This works on SQL Server 2000 and above

    IF OBJECTPROPERTY(OBJECT_ID('{your_trigger}'), 'IsTrigger') = 1
    BEGIN
        ...
    END
    

    Note that the naive converse doesn't work reliably:

    -- This doesn't work for checking for absense
    IF OBJECTPROPERTY(OBJECT_ID('{your_trigger}'), 'IsTrigger') <> 1
    BEGIN
        ...
    END
    

    ...because if the object doesn't exist at all, OBJECTPROPERTY returns NULL, and NULL is (of course) not <> 1 (or anything else).

    On SQL Server 2005 or later, you could use COALESCE to deal with that, but if you need to support SQL Server 2000, you'll have to structure your statement to deal with the three possible return values: NULL (the object doesn't exist at all), 0 (it exists but is not a trigger), or 1 (it's a trigger).

    0 讨论(0)
提交回复
热议问题