如何查找与SQL Server相关的所有触发器?

时间:2012-09-10 07:17:39

标签: sql-server sql-server-2008

我为 SQL Server 中的表创建了一个触发器,它对我有用。

我的问题是:如何找到并修改它?

我使用此查询来查找触发器:

select * from sys.triggers

这可以找到所有触发器,但是如何打开它并更改触发器?

15 个答案:

答案 0 :(得分:45)

您只需使用SSMS即可完成此操作。只需转到表名并展开“触发器”节点,即可查看与该表关联的触发器列表。右键单击以修改触发器。 enter image description here

答案 1 :(得分:34)

select so.name, text
from sysobjects so, syscomments sc
where type = 'TR'
and so.id = sc.id
and text like '%YourTableName%'

通过这种方式,您可以列出与给定表关联的所有触发器。

答案 2 :(得分:9)

这可能很有用

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

这样你只需要插入表名,查询就会获取你需要的所有触发器

答案 3 :(得分:6)

select m.definition from sys.all_sql_modules m inner join  sys.triggers t
on m.object_id = t.object_id 

这里只需复制定义并更改触发器。

此外,您可以转到SSMS并展开您的数据库,然后在Programmability下展开Database Triggeres,然后右键单击特定触发器并单击修改,您也可以更改。

答案 4 :(得分:5)

使用sp_helptrigger查找关联表的触发器列表

答案 5 :(得分:3)

在桌面上查找触发器:

select so.name, text
from sysobjects so, syscomments sc
where type = 'TR'
and so.id = sc.id
and text like '%TableName%'

你可以找到参考表的商店程序:

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%yourtablename%'

答案 6 :(得分:2)

使用此查询,您可以在所有表​​和所有视图中找到所有触发器。

    ;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 

答案 7 :(得分:0)

下面的简单查询

select (select [name] from  sys.tables where [object_id] = tr.parent_id ) as TableName ,*  from sys.triggers tr

答案 8 :(得分:0)

从information_schema.TRIGGERS中选择*;

答案 9 :(得分:0)

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'

这将为您提供指定表

上的所有触发器

答案 10 :(得分:0)

您可以通过以下查询查看与数据库相关的所有触发器

select * from sys.triggers

对于开放式触发器,您可以使用以下语法

sp_helptext 'dbo.trg_InsertIntoUserTable'

答案 11 :(得分:0)

尝试使用:

select * from sys.objects where type='tr' and name like '%_Insert%'

答案 12 :(得分:0)

您可以使用以下命令打开触发器 sp_helptext yourtriggername

答案 13 :(得分:0)

 select o1.name as trigger_name,o2.name as table_name from sys.objects o1 
 join sys.objects o2 on 
 o1.parent_object_id=o2.object_id     
 where o1.type='TR' 
 and o2.name='my_table'
 
  

答案 14 :(得分:0)

select   B.name 
from     sys.objects    A
join     sys.triggers   B
on     A.object_id    =    B.parent_id
where    A.name    ='Table_name' /*Table Name*/