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

后端 未结 19 755
再見小時候
再見小時候 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 18:04

    Here you go.

        SELECT
        [so].[name] AS [trigger_name],
        USER_NAME([so].[uid]) AS [trigger_owner],
        USER_NAME([so2].[uid]) AS [table_schema],
        OBJECT_NAME([so].[parent_obj]) AS [table_name],
        OBJECTPROPERTY( [so].[id], 'ExecIsUpdateTrigger') AS [isupdate],
        OBJECTPROPERTY( [so].[id], 'ExecIsDeleteTrigger') AS [isdelete],
        OBJECTPROPERTY( [so].[id], 'ExecIsInsertTrigger') AS [isinsert],
        OBJECTPROPERTY( [so].[id], 'ExecIsAfterTrigger') AS [isafter],
        OBJECTPROPERTY( [so].[id], 'ExecIsInsteadOfTrigger') AS [isinsteadof],
        OBJECTPROPERTY([so].[id], 'ExecIsTriggerDisabled') AS [disabled] 
    FROM sysobjects AS [so]
    INNER JOIN sysobjects AS so2 ON so.parent_obj = so2.Id
    WHERE [so].[type] = 'TR'
    

    A couple of things here...

    Also I see that you were attempting to pull the parent tables schema information, I believe in order to do so you would also need to join the sysobjects table on itself so that you can correctly get the schema information for the parent table. the query above does this. Also the sysusers table wasn't needed in the results so that Join has been removed.

    tested with SQL 2000, SQL 2005, and SQL 2008 R2

    0 讨论(0)
  • 2020-11-28 18:05
    SELECT
       ServerName   = @@servername,
       DatabaseName = db_name(),
       SchemaName   = isnull( s.name, '' ),
       TableName    = isnull( o.name, 'DDL Trigger' ),
       TriggerName  = t.name, 
       Defininion   = object_definition( t.object_id )
    
    FROM sys.triggers t
       LEFT JOIN sys.all_objects o
          ON t.parent_id = o.object_id
       LEFT JOIN sys.schemas s
          ON s.schema_id = o.schema_id
    ORDER BY 
       SchemaName,
       TableName,
       TriggerName
    
    0 讨论(0)
  • 2020-11-28 18:09

    And what do you think about this: Very short and neat :)

    SELECT OBJECT_NAME(parent_id) Table_or_ViewNM,
          name TriggerNM,
          is_instead_of_trigger,
          is_disabled
    FROM sys.triggers
    WHERE parent_class_desc = 'OBJECT_OR_COLUMN'
    ORDER BY OBJECT_NAME(parent_id),
    Name ;
    
    0 讨论(0)
  • 2020-11-28 18:14

    this may help.

    SELECT DISTINCT o.[name] AS [Table]
    FROM    [sysobjects] o
    JOIN    [sysobjects] tr
        ON  o.[id] = tr.[parent_obj]
    WHERE   tr.[type] = 'tr'
    ORDER BY [Table]
    
    Get a list of tables and all their triggers.
    
    SELECT DISTINCT o.[name] AS [Table], tr.[name] AS [Trigger]
    FROM    [sysobjects] o
    JOIN    [sysobjects] tr
        ON  o.[id] = tr.[parent_obj]
    WHERE   tr.[type] = 'tr'
    ORDER BY [Table], [Trigger]
    
    0 讨论(0)
  • 2020-11-28 18:15

    Use This Query :

        SELECT     
            DB_NAME() AS DataBaseName,  
            S.Name AS SchemaName,               
            T.name AS TableName,
            dbo.SysObjects.Name AS TriggerName,
            dbo.sysComments.Text AS SqlContent,
        FROM dbo.SysObjects 
        INNER JOIN dbo.sysComments ON dbo.SysObjects.ID = dbo.sysComments.ID
        INNER JOIN sys.tables AS T ON sysobjects.parent_obj = t.object_id 
        INNER JOIN sys.schemas AS S ON t.schema_id = s.schema_id 
        WHERE dbo.SysObjects.xType = 'TR' 
            AND dbo.SysObjects.Name LIKE 'Permit_AfterInsert' ---- <----- HERE
    
    0 讨论(0)
  • 2020-11-28 18:18

    C# Cribs: I ended up with this super generic one liner. Hope this is useful to both the original poster and/or people who just typed the same question I did into Google:

    SELECT TriggerRecord.name as TriggerName,ParentRecord.name as ForTableName 
    FROM sysobjects TriggerRecord 
    INNER JOIN sysobjects ParentRecord ON TriggerRecord.parent_obj=ParentRecord.id 
    WHERE TriggerRecord.xtype='TR'
    

    Query Characteristics:

    • Usable with any SQL database (i.e. Initial Catalog)
    • Self explanatory
    • One single statement
    • Pasteable directly into most IDE's for most languages
    0 讨论(0)
提交回复
热议问题