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

时间:2017-07-20 20:56:03

标签: database triggers sybase sybase-ase

我试图在Sybase ASE 16.0中查找给定模式中的表上定义的所有触发器,并且可以在与给定表不同的模式中定义触发器(用户具有所有必需的权限)。例如,下表将分别在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
/

有没有办法同时获取此表中定义的触发器详细信息(名称和架构)?

我试过以下:

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'
  • 问题:每个触发类型仅提供1个触发器(1个插入,1个删除,1个更新触发器)
sp_helptrigger 'tblAllTypesTriggers'
  • 问题:仅提供在表上定义的触发器名称,但不提供其架构
sp_depends 'tblAllTypesTriggers' 
  • 问题:还列出了引用此表的其他表的触发器

1 个答案:

答案 0 :(得分:0)

sysobjects。{instrig / deltrig / updtrig} 列是对好人的回归。表格每种类型的触发器最多只能有1个。 [是的,这些列仍在使用,但仅适用于表格所有者创建的第一个触发器;或instead of触发器中视图的所有者。]

请注意,对于 sysobjects.type =' TR' 条目, deltrig 列包含触发器的基表的id属于...来自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

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

使用您的示例代码(并将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 可以从一些编辑中受益,例如,显示每个触发器的所有者/架构。]

一个快速的,非常热门的查询来回答您的问题:

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

sysobjects sysconstraints sp_helptrigger 的来源之间,你应该能够按照你希望的方式对数据进行切片。