The sysobjects.{instrig/deltrig/updtrig} columns are a throwback to the good ol' days when a table could only have at most 1 of each type of trigger. [Yes, these columns are still used but only for the first trigger created by the table's owner; or the view's owner in the case of instead of
triggers.]
Keep in mind that for sysobjects.type='TR' entries, the deltrig column contains the id of the base table to which the trigger belongs ... from the sysobjects.deltrig column description:
deltrig: Stored procedure ID of a delete trigger if the entry is a table. Table ID if the entry is a trigger
Unfortunately, it gets a bit more complicated in that additional triggers (eg, created by the non-table owner in this case) also get an associated row added to sysconstraints (sysconstraints.constrid = object_id(>trigger_name<)), with the sysconstraints.status column (a bitmap) designating whether the trigger is for insert, update and/or delete.
Using your example code (and replacing s1
with markp
), this should give you an idea of what you're up against:
select id,
left(name,30) as objname,
type,
left(user_name(uid),10) as 'owner',
deltrig,
instrig,
updtrig
from sysobjects
where name like 'tblAll%'
order by type,uid
go
id objname type owner deltrig instrig updtrig
----------- ------------------------------ ---- ---------- ----------- ----------- -----------
752002679 tblAllTypesTriggers_6 TR dbo 736002622 0 0
816002907 tblAllTypesTriggers_6 TR markp 736002622 0 0
736002622 tblAllTypesTriggers U dbo 0 752002679 0
-- here we see the 2x triggers (type = TR) have deltrig = 736002622 = id of the table (type = U)
select * from sysconstraints where tableid = object_id('tblAllTypesTriggers')
go
colid constrid tableid error status spare2
------ ----------- ----------- ----------- ----------- -----------
0 816002907 736002622 0 1024 0
-- here we see markp's trigger (constrid = 816002907) is associated with
-- the dbo's table (tableid = 736002622), with status & 1024 = 1024
-- indicating that this is a 'insert' trigger
NOTE: You can derive all of the above from the source code for sp_helptrigger. ("Duh, Mark!" ?) [And yes, the default sp_helptrigger could benefit from some edits, eg, showing the owner/schema of each trigger.]
A quick, off-the-top-of-my-head query to answer your question:
select left(o1.name,30) as tabname,
left(user_name(o1.uid),10) as tabowner,
left(o2.name,30) as trigname,
left(user_name(o2.uid),10) as trigowner
from sysobjects o1,
sysobjects o2
where o1.name = 'tblAllTypesTriggers'
and o1.type = 'U'
and o2.deltrig = o1.id
and o2.type = 'TR'
order by 1,2,4,3
go
tabname tabowner trigname trigowner
------------------------------ ---------- ------------------------------ ----------
tblAllTypesTriggers dbo tblAllTypesTriggers_6 dbo
tblAllTypesTriggers dbo tblAllTypesTriggers_6 markp
Between sysobjects, sysconstraints and the source for sp_helptrigger you should be able to slice-n-dice the data as you wish eh.