Need to list all triggers in SQL Server database with table name and table's schema

后端 未结 19 753
再見小時候
再見小時候 2020-11-28 17:12

I need to list all triggers in SQL Server database with table name and table\'s schema.

I\'m almost there with this:

SELECT trigger_name = name, trig         


        
相关标签:
19条回答
  • 2020-11-28 17:58

    I had the same task recently and I used the following for sql server 2012 db. Use management studio and connect to the database you want to search. Then execute the following script.

    Select 
    [tgr].[name] as [trigger name], 
    [tbl].[name] as [table name]
    
    from sysobjects tgr 
    
    join sysobjects tbl
    on tgr.parent_obj = tbl.id
    
    WHERE tgr.xtype = 'TR'
    
    0 讨论(0)
  • 2020-11-28 18:00

    The just above code is incorrect as shown:

    SELECT 
        sysobjects.name AS trigger_name 
        --,USER_NAME(sysobjects.uid) AS trigger_owner 
        --,s.name AS table_schema 
        --,OBJECT_NAME(parent_obj) AS table_name 
        --,OBJECTPROPERTY( id, 'ExecIsUpdateTrigger') AS isupdate 
        --,OBJECTPROPERTY( id, 'ExecIsDeleteTrigger') AS isdelete 
        --,OBJECTPROPERTY( id, 'ExecIsInsertTrigger') AS isinsert 
        --,OBJECTPROPERTY( id, 'ExecIsAfterTrigger') AS isafter 
        --,OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger') AS isinsteadof 
        --,OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') AS [disabled] 
    FROM sysobjects 
    /*
    INNER JOIN sysusers 
        ON sysobjects.uid = sysusers.uid 
    */  
    INNER JOIN sys.tables t 
        ON sysobjects.parent_obj = t.object_id 
    
    INNER JOIN sys.schemas s 
        ON t.schema_id = s.schema_id 
    WHERE sysobjects.type = 'TR' 
    EXCEPT
    SELECT OBJECT_NAME(parent_id) as Table_Name FROM sys.triggers
    
    0 讨论(0)
  • 2020-11-28 18:01

    If you are looking for ALL triggers, remember MS-SQL has both SQL-based triggers (sysobjects.type = 'TR') and CLR-based triggers (sysobjects.type = 'TA').

    0 讨论(0)
  • 2020-11-28 18:02
    SELECT 
         sysobjects.name AS trigger_name   ,OBJECT_NAME(parent_obj) AS table_name ,s.name AS table_schema 
        ,USER_NAME(sysobjects.uid) AS trigger_owner 
    
    
        ,OBJECTPROPERTY( id, 'ExecIsUpdateTrigger') AS isupdate 
        ,OBJECTPROPERTY( id, 'ExecIsDeleteTrigger') AS isdelete 
        ,OBJECTPROPERTY( id, 'ExecIsInsertTrigger') AS isinsert 
        ,OBJECTPROPERTY( id, 'ExecIsAfterTrigger') AS isafter 
        ,OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger') AS isinsteadof 
        ,OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') AS [disabled] 
    FROM sysobjects 
    
    INNER JOIN sysusers 
        ON sysobjects.uid = sysusers.uid 
    
    INNER JOIN sys.tables t 
        ON sysobjects.parent_obj = t.object_id 
    
    INNER JOIN sys.schemas s 
        ON t.schema_id = s.schema_id 
    
    WHERE sysobjects.type = 'TR' 
    

    this working for me

    0 讨论(0)
  • 2020-11-28 18:03

    This is what I use (usually wrapped in something I stuff in Model):

    Select
      [Parent] = Left((Case When Tr.Parent_Class = 0 Then '(Database)' Else Object_Name(Tr.Parent_ID) End), 32),
      [Schema] = Left(Coalesce(Object_Schema_Name(Tr.Object_ID), '(None)'), 16),
      [Trigger name] = Left(Tr.Name, 32), 
      [Type] = Left(Tr.Type_Desc, 3), -- SQL or CLR
      [MS?] = (Case When Tr.Is_MS_Shipped = 1 Then 'X' Else ' ' End),
      [On?] = (Case When Tr.Is_Disabled = 0 Then 'X' Else ' ' End),
      [Repl?] = (Case When Tr.Is_Not_For_Replication = 0 Then 'X' Else ' ' End),
      [Event] = Left((Case When Tr.Parent_Class = 0 
                           Then (Select Top 1 Left(Te.Event_Group_Type_Desc, 40)
                                 From Sys.Trigger_Events As Te
                                 Where Te.Object_ID = Tr.Object_ID)
                           Else ((Case When Tr.Is_Instead_Of_Trigger = 1 Then 'Instead Of ' Else 'After ' End)) +
                                 SubString(Cast((Select [text()] = ', ' + Left(Te.Type_Desc, 1) + Lower(SubString(Te.Type_Desc, 2, 32)) +
                                                        (Case When Te.Is_First = 1 Then ' (First)' When Te.Is_Last = 1 Then ' (Last)' Else '' End)
                                                 From Sys.Trigger_Events As Te
                                                 Where Te.Object_ID = Tr.Object_ID
                                                 Order By Te.[Type]
                                                 For Xml Path ('')) As Character Varying), 3, 60) End), 60)
      -- If you like: 
      -- , [Get text with] = 'Select Object_Definition(' + Cast(Tr.Object_ID As Character Varying) + ')'
    From 
      Sys.Triggers As Tr
    Order By
      Tr.Parent_Class, -- database triggers first
      Parent -- alphabetically by parent
    

    As you see it is a skosh more McGyver, but I think it's worth it:

    Parent                           Schema           Trigger name                     Type MS?  On?  Repl? Event
    -------------------------------- ---------------- -------------------------------- ---- ---- ---- ----- -----------------------------------------
    (Database)                       (None)           ddlDatabaseTriggerLog            SQL            X     DDL_DATABASE_LEVEL_EVENTS
    Employee                         HumanResources   dEmployee                        SQL       X          Instead Of Delete
    Person                           Person           iuPerson                         SQL       X          After Insert, Update
    PurchaseOrderDetail              Purchasing       iPurchaseOrderDetail             SQL       X    X     After Insert
    PurchaseOrderDetail              Purchasing       uPurchaseOrderDetail             SQL       X    X     After Update
    PurchaseOrderHeader              Purchasing       uPurchaseOrderHeader             SQL       X    X     After Update
    SalesOrderDetail                 Sales            iduSalesOrderDetail              SQL       X    X     After Insert, Update, Delete
    SalesOrderHeader                 Sales            uSalesOrderHeader                SQL       X          After Update (First)
    Vendor                           Purchasing       dVendor                          SQL       X          Instead Of Delete
    WorkOrder                        Production       iWorkOrder                       SQL       X    X     After Insert
    WorkOrder                        Production       uWorkOrder                       SQL       X    X     After Update
    

    (Scroll right to see the final and most useful column)

    0 讨论(0)
  • 2020-11-28 18:03
    SELECT
        OBJECT_NAME(PARENT_OBJECT_ID) AS PARENT_TABLE,
        OBJECT_NAME(OBJECT_ID) TRIGGER_TABLE,
        *
    FROM
    SYS.OBJECTS
    WHERE TYPE = 'TR'
    
    0 讨论(0)
提交回复
热议问题