| Author |
Topic |
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-14 : 04:57:08
|
| How can I know how many triggers in my database are causing Insert, update or delete in a particular table? For example: I am having my table tbl_Comments. Records in it are inserted when couple of other tables (which I don't know) are updated. How can I get the name of these tables on whose insert/update my table "tbl_Comments" is getting inserted/updated. Hope I have made my query clear? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 05:01:34
|
| Cant you view the content of the trigger using sp_helptext 'trigger_name'?MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-14 : 05:09:15
|
| One method is to have a varchar column in your table called Modified_By and populate it with name of table whose trigger action caused the insertion action onto table in trigger code. |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-14 : 05:19:22
|
quote: Originally posted by visakh16 One method is to have a varchar column in your table called Modified_By and populate it with name of table whose trigger action caused the insertion action onto table in trigger code.
I am not able to get what you mean by "onto table in trigger code.". My objective is to find triggers which are causing insert updates to my table. I have identified 4 tables which have triggers updating my table. I want to know how many more tables are there which will cause insert/update to my table.Madhi: I am looking for triggers causing updates. Not sure what you mean by "Cant you view the content of the trigger using sp_helptext 'trigger_name'?" I myself is looking for 'trigger_name'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-14 : 05:31:15
|
quote: Originally posted by sunil
quote: Originally posted by visakh16 One method is to have a varchar column in your table called Modified_By and populate it with name of table whose trigger action caused the insertion action onto table in trigger code.
I am not able to get what you mean by "onto table in trigger code.". My objective is to find triggers which are causing insert updates to my table. I have identified 4 tables which have triggers updating my table. I want to know how many more tables are there which will cause insert/update to my table.Madhi: I am looking for triggers causing updates. Not sure what you mean by "Cant you view the content of the trigger using sp_helptext 'trigger_name'?" I myself is looking for 'trigger_name'.
ok. imagine this scenario. you have a table t which is populated by three triggers on other three tables (t1,t2,t3). you need to track which tables trigger action caused a particular records insert in t.let trig1,trig2,trig3 be triggers on t1 t2 t3 which causes insert on t then have a new column modified_by in t of type varchar(50) (say)then trig1 you will haveCREATE TRIGGER trig1 on tAFTER INSERT/UPDATE,..AS....other code...INSERT INTO t (other fields,..,modified_by)SELECT other values, 't1 insertion'FROM INSERTEDGOsimilarly you will have for trig2CREATE TRIGGER trig2 on tAFTER INSERT/UPDATE,..AS....other code...INSERT INTO t (other fields,..,modified_by)SELECT other values, 't2 insertion'FROM INSERTEDGOand so onif you look at records of t you will havefield1 field2 .... modified_byval1 val2 ...... t1 insertionval4 val5 ...... t2 insertionval6 val7 ...... t1 insertionval1 val2....... t3 insertionval1 val2 ...... t2 insertionval1 val2 ...... t1 insertion.... |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-14 : 05:45:46
|
Oh! It seems I am not good at writing. You are partially correct Visakh. My problem is that I don't know(numbers) by how many triggers my table t is getting populated. Till now i am manually searching tables to find triggers updating my table. I am looking for a quick SQL way of determining number of triggers updating my table. Hope this time I am spot on with my question. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 06:00:36
|
| See if this helpsselect object_name(s.id) as trigger_name,c.text from syscomments c inner join sysobjects son c.id=s.id where xtype='tr' and c.text like '%update%tbl_Comments%'MadhivananFailing to plan is Planning to fail |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-14 : 06:20:49
|
quote: Originally posted by madhivanan See if this helpsselect object_name(s.id) as trigger_name,c.text from syscomments c inner join sysobjects son c.id=s.id where xtype='tr' and c.text like '%update%tbl_Comments%'MadhivananFailing to plan is Planning to fail
Thanks, It lists all triggers which updates my table. I modified it to get triggers which do Insert also.select object_name(s.id) as trigger_name,c.text from syscomments c inner join sysobjects son c.id=s.id where xtype='tr' and c.text like '%update%Emp1%' unionselect object_name(s.id) as trigger_name,c.text from syscomments c inner join sysobjects son c.id=s.id where xtype='tr' and c.text like '%Insert%Emp1%' |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 06:27:33
|
| Alsoselect object_name(s.id) as trigger_name,c.text from syscomments c inner join sysobjects son c.id=s.id where xtype='tr' and (c.text like '%update%Emp1%' or c.text like '%Insert%Emp1%') MadhivananFailing to plan is Planning to fail |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-14 : 06:35:08
|
| I saw execution plan for both the queries with union and suggested by madhi. How do I know which one is efficient? Also in general, how does one interpret execution plan. It goes over my head when I see it. Any references as to how to interpret execution plan and choose best way. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 07:21:43
|
quote: Originally posted by sunil I saw execution plan for both the queries with union and suggested by madhi. How do I know which one is efficient? Also in general, how does one interpret execution plan. It goes over my head when I see it. Any references as to how to interpret execution plan and choose best way.
Do search here http://www.sql-server-performance.comYou can find lot of articlesMadhivananFailing to plan is Planning to fail |
 |
|
|
|