How to find all trigger associated with a table with SQL Server?

后端 未结 14 1501
日久生厌
日久生厌 2020-12-13 17:46

I created a trigger for a table in SQL Server and it works for me.

My problem is: How do find it and modify it?

I use this query to find my triggers

相关标签:
14条回答
  • 2020-12-13 17:53
    select t.name as TriggerName,m.definition,is_disabled 
    from sys.all_sql_modules m 
    inner join  
    sys.triggers t
    on m.object_id = t.object_id 
    inner join sys.objects o
    on o.object_id = t.parent_id
    Where o.name = 'YourTableName'
    

    This will give you all triggers on a Specified Table

    0 讨论(0)
  • 2020-12-13 17:54

    With this query you can find all Trigger in all tables and all views.

        ;WITH
            TableTrigger
            AS
            (
                Select 
                    Object_Kind = 'Table',
                    Sys.Tables.Name As TableOrView_Name , 
                    Sys.Tables.Object_Id As Table_Object_Id ,
                    Sys.Triggers.Name As Trigger_Name, 
                    Sys.Triggers.Object_Id As Trigger_Object_Id 
                From Sys.Tables 
                INNER Join Sys.Triggers On ( Sys.Triggers.Parent_id = Sys.Tables.Object_Id )
                Where ( Sys.Tables.Is_MS_Shipped = 0 )
            ),
            ViewTrigger
            AS
            (
                Select 
                    Object_Kind = 'View',
                    Sys.Views.Name As TableOrView_Name , 
                    Sys.Views.Object_Id As TableOrView_Object_Id ,
                    Sys.Triggers.Name As Trigger_Name, 
                    Sys.Triggers.Object_Id As Trigger_Object_Id 
                From Sys.Views 
                INNER Join Sys.Triggers On ( Sys.Triggers.Parent_id = Sys.Views.Object_Id )
                Where ( Sys.Views.Is_MS_Shipped = 0 )
            ),
            AllObject
            AS
            (
                SELECT * FROM TableTrigger
    
                Union ALL
    
                SELECT * FROM ViewTrigger
            )
    
    
        Select 
            * 
        From AllObject
        Order By Object_Kind, Table_Object_Id 
    
    0 讨论(0)
  • 2020-12-13 17:58
    select m.definition from sys.all_sql_modules m inner join  sys.triggers t
    on m.object_id = t.object_id 
    

    Here just copy the definition and alter the trigger.

    Else you can just goto SSMS and Expand the your DB and under Programmability expand Database Triggeres then right click on the specific trigger and click modify there also you can change.

    0 讨论(0)
  • 2020-12-13 17:59

    select * from information_schema.TRIGGERS;

    0 讨论(0)
  • 2020-12-13 18:01

    This might be useful

    SELECT 
     t.name AS TableName,
     tr.name AS TriggerName  
    FROM sys.triggers tr
    INNER JOIN sys.tables t ON t.object_id = tr.parent_id
    WHERE 
    t.name in ('TABLE_NAME(S)_GOES_HERE');
    

    This way you just have to plugin the name of tables and the query will fetch all the triggers you need

    0 讨论(0)
  • 2020-12-13 18:03

    Much simple query below

    select (select [name] from  sys.tables where [object_id] = tr.parent_id ) as TableName ,*  from sys.triggers tr
    
    0 讨论(0)
提交回复
热议问题