55

I created a trigger for a table in SQL Server and it works for me.

My problem is: How do find it and modify it?

I use this query to find my triggers:

select * from sys.triggers

This find all triggers, but how to open it and change the triggers?

JeanValjean
  • 17,172
  • 23
  • 113
  • 157
ar.gorgin
  • 4,765
  • 12
  • 61
  • 100

16 Answers16

60

You can do this simply with SSMS. Just go to your table name and expand the Triggers node to view a list of triggers associated with that table. Right click to modify your trigger. enter image description here

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Buzz
  • 6,030
  • 4
  • 33
  • 47
53
select so.name, text
from sysobjects so, syscomments sc
where type = 'TR'
and so.id = sc.id
and text like '%YourTableName%'

This way you can list out all the triggers associated with the given table.

Maslow
  • 18,464
  • 20
  • 106
  • 193
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
  • 1
    This could potentially return _too many rows_ case the %YoutTableName% is not just to represent a table. Example: **Product** -< **Product**Promotion >- Promotion – pfonseca Mar 28 '16 at 10:07
  • 1
    This is a great answer, because it should work across all development environments. I needed to find a trigger that wasn't showing under my database triggers folder in SSMS, connected to an Azure hosted database. – Jason D. Jan 13 '17 at 21:31
23

This might be useful

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

This way you just have to plugin the name of tables and the query will fetch all the triggers you need

Saurabh
  • 336
  • 3
  • 9
7
select m.definition from sys.all_sql_modules m inner join  sys.triggers t
on m.object_id = t.object_id 

Here just copy the definition and alter the trigger.

Else you can just goto SSMS and Expand the your DB and under Programmability expand Database Triggeres then right click on the specific trigger and click modify there also you can change.

AnandPhadke
  • 13,160
  • 5
  • 26
  • 33
6

use sp_helptrigger to find the triggerlist for the associated tables

Padhu
  • 97
  • 3
  • 10
4

find triggers on table:

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

and you can find store procedure which has reference of table:

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%yourtablename%'
thaavik
  • 3,257
  • 2
  • 18
  • 25
4
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*/
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Jeroen Steenbeeke Jun 10 '21 at 08:10
3

Go through

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

This URL have set of queries by which you can get the list of triggers associated with particular table.

I believe you are working in sqlserver following are the steps to get modify triggers

To modify a trigger

  1. Expand a server group, and then expand a server.

  2. Expand Databases, expand the database in which the table containing the trigger belongs, and then click Tables.

  3. In the details pane, right-click the table on which the trigger exists, point to All Tasks, and then click Manage Triggers.

  4. In Name, select the name of the trigger.

  5. Change the text of the trigger in the Text field as necessary. Press CTRL+TAB to indent the text of a SQL Server Enterprise Manager trigger.

  6. To check the syntax of the trigger, click Check Syntax.

Community
  • 1
  • 1
3

With this query you can find all Trigger in all tables and all views.

    ;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 
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
2
 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'
 
  
YakovGdl35
  • 331
  • 3
  • 4
1

You Can View All trigger related to your database by below query

select * from sys.triggers

And for open trigger you can use below syntax

sp_helptext 'dbo.trg_InsertIntoUserTable'
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
Anuj Kumar
  • 29
  • 1
  • 6
0

Much simple query below

select (select [name] from  sys.tables where [object_id] = tr.parent_id ) as TableName ,*  from sys.triggers tr
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
dilipkumar katre
  • 128
  • 1
  • 12
0

select * from information_schema.TRIGGERS;

Mrinal
  • 125
  • 5
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'

This will give you all triggers on a Specified Table

0

Try to Use:

select * from sys.objects where type='tr' and name like '%_Insert%'
Fezal halai
  • 756
  • 7
  • 14
0

you can open your trigger with sp_helptext yourtriggername

danish
  • 21
  • 1