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

后端 未结 19 938
再見小時候
再見小時候 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

提交回复
热议问题