How to find triggers along with schemas defined on a table in Sybase ASE 16.0?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 19:53:14

问题


I am trying to find all the triggers defined on a table in a given schema in Sybase ASE 16.0, and the triggers can be defined in different schema than the given table (User has all required permissions). Example, below table will be created in dbo schema (default) and the triggers in dbo and s1 schema respectively.

CREATE TABLE tblAllTypesTriggers ( 
    "Id"            int NOT NULL primary key,
    "Name"          varchar(30),
    "Salary"        int,
    "Gender"        varchar(10),
    "DepartmentId"  int 
    )
LOCK ALLPAGES
/

CREATE TRIGGER tblAllTypesTriggers_6
ON tblAllTypesTriggers 
FOR INSERT 
AS 
BEGIN 
 -- do something
END
/

CREATE TRIGGER s1.tblAllTypesTriggers_6
ON tblAllTypesTriggers 
FOR INSERT 
AS 
BEGIN 
 -- do something
END
/

Is there any way to get both the trigger details (name and schema) that are defined on this table?

I have tried following:

select so2.name, so2.uid from sysobjects so1, sysobjects so2 where
(so2.id = so1.deltrig or so2.id = so1.instrig or so2.id=so1.updtrig or
so2.id=so1.seltrig)  and so1.name= 'tblAllTypesTriggers'
  • Issue: only gives 1 trigger per trigger type (1 insert, 1 delete, 1 update trigger)
sp_helptrigger 'tblAllTypesTriggers'
  • Issue: only gives trigger names defined on table, but not their schema
sp_depends 'tblAllTypesTriggers' 
  • Issue: also lists triggers on other tables that refer this table

回答1:


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.



来源:https://stackoverflow.com/questions/45225009/how-to-find-triggers-along-with-schemas-defined-on-a-table-in-sybase-ase-16-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!