如何在 Sybase ASE 16.0 中查找触发器以及在 table 上定义的模式?

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

我正在尝试在 Sybase ASE 16.0 的给定模式中查找在 table 上定义的所有触发器,并且可以在与给定 table 不同的模式中定义触发器(用户有所有必需的权限)。例如,下面的 table 将分别在 dbo 架构(默认)和 dbo 和 s1 架构中创建触发器。

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
/

有什么方法可以获取在此 table 上定义的触发器详细信息(名称和架构)?

我试过以下方法:

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'
sp_helptrigger 'tblAllTypesTriggers'
sp_depends 'tblAllTypesTriggers' 

sysobjects.{instrig/deltrig/updtrig} 列是对美好时光的倒退,当时 table 每个列最多只能有 1 个触发器类型。 [是的,这些列仍在使用,但仅用于 table 所有者创建的第一个触发器;或者在 instead of 触发器的情况下视图的所有者。]

请记住,对于 sysobjects.type='TR' 条目,deltrig 列包含的 ID触发器所属的基数 table ... 来自 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

不幸的是,它变得有点复杂,因为额外的触发器(例如,在这种情况下由非table所有者创建)也会将关联的行添加到sysconstraints (sysconstraints.constrid = object_id(>trigger_name<)), 与 sysconstraints.status列(位图)指定触发器是否用于插入、更新and/or删除。

使用您的示例代码(并将 s1 替换为 markp),这应该让您了解您面临的问题:

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

注意:您可以从 sp_helptrigger 的源代码中导出上述所有内容。 ("Duh, Mark!" ?) [是的,默认 sp_helptrigger 可以从一些编辑中获益,例如,显示每个触发器的 owner/schema。]

回答您问题的快速、即兴查询:

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

sysobjectssysconstraintssp_helptrigger 的来源之间你应该能够按照您的意愿对数据进行切片和切块,嗯。