需要在表名和表格模式中列出SQL Server数据库中的所有触发器

我需要列出SQL Server数据库中的所有触发器与表名和表的模式。

我几乎在这里:

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

我只需要获取表格的模式。

这里有一个方法:

 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' 

编辑 :注释join到sysusers的查询工作AdventureWorks2008。

 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' 

编辑2 :对于SQL 2000

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

干得好。

  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' 

这里有几件事

此外,我看到你正试图拉父表模式信息,我相信为了做到这一点,你也需要join自己的sysobjects表,以便您可以正确地获取父表的架构信息。 上面的查询是这样做的。 此外,结果中不需要sysusers表,这样Join就被删除了。

使用SQL 2000,SQL 2005和SQL 2008 R2进行testing

我最近有同样的任务,我使用以下的SQL Server 2012数据库。 使用pipe理工作室并连接到您要search的数据库。 然后执行下面的脚本。

 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' 

你也可以得到触发器的主体如下:

 SELECT o.[name], c.[text] FROM sys.objects AS o INNER JOIN sys.syscomments AS c ON o.object_id = c.id WHERE o.[type] = 'TR' 
 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 

你怎么看这个:非常短而整齐:)

 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 ; 
 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' 

这为我工作

使用这个查询:

 SELECT OBJECT_NAME(parent_id) as Table_Name, * FROM [Database_Name].sys.triggers 

这很简单和有用。

这可能有帮助。

 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] 

使用此查询:

  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 

这是我使用的(通常包裹在我在模型中的东西):

 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 

正如你所看到的,这是一个更多的McGyver,但我认为这是值得的:

 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 XX After Insert PurchaseOrderDetail Purchasing uPurchaseOrderDetail SQL XX After Update PurchaseOrderHeader Purchasing uPurchaseOrderHeader SQL XX After Update SalesOrderDetail Sales iduSalesOrderDetail SQL XX After Insert, Update, Delete SalesOrderHeader Sales uSalesOrderHeader SQL X After Update (First) Vendor Purchasing dVendor SQL X Instead Of Delete WorkOrder Production iWorkOrder SQL XX After Insert WorkOrder Production uWorkOrder SQL XX After Update 

(向右滚动以查看最终和最有用的列)

上面的代码是不正确的,如下所示:

  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 
 SELECT tbl.name as Table_Name,trig.name as Trigger_Name,trig.is_disabled FROM [sys].[triggers] as trig inner join sys.tables as tbl on trig.parent_id = tbl.object_id 

如果您正在查找ALL触发器,请记住MS-SQL同时具有基于SQL的触发器( sysobjects.type = 'TR' )和基于CLR的触发器( sysobjects.type = 'TA' )。

一个难点是文本或说明有换行。 为了让它更具表格性,我把它弄成了一个更具表格的东西,就是在SELECT子句中添加一个HTML文字,将所有内容复制并粘贴到记事本中,保存为html扩展名,在浏览器中打开,然后复制并粘贴到电子表格中。 例

 SELECT obj.NAME AS TBL,trg.name,sm.definition,'<br>' FROM SYS.OBJECTS obj LEFT JOIN (SELECT trg1.object_id,trg1.parent_object_id,trg1.name FROM sys.objects trg1 WHERE trg1.type='tr' AND trg1.name like 'update%') trg ON obj.object_id=trg.parent_object_id LEFT JOIN (SELECT sm1.object_id,sm1.definition FROM sys.sql_modules sm1 where sm1.definition like '%suser_sname()%') sm ON trg.object_id=sm.object_id WHERE obj.type='u' ORDER BY obj.name; 

你可能还需要用制表符来把这个描述合并到一个字段中,但是至less它会在一行中,我觉得这很有帮助。