Sybase: Enabling and disabling triggers

If you want to disable a trigger, you can of course just drop it. But when you need it again, you’ll have to recreate it. If you just want to disable the trigger because you’re copying data to the table and this data has already been preprocessed and the triggered is just getting in the way, you can disable it with the alter table command:

1> alter table mytable disable trigger
2> go

This will disable all triggers on the table. If you only want to disable a single trigger, you can use the following:

1> alter table mytable disable trigger mytriggername
2> go

To enable all triggers:

1> alter table mytable enable trigger
2> go

To enable a specific trigger:

1> alter table mytable enable trigger mytriggername
2> go

If you want to check whether the triggers are enabled or disabled, you’ll have to check the sysobjects entry for this table. The column sysstat2 contains the information you’re looking for:

select 
object_name(instrig) as InsertTriggerName,
case sysstat2 & 1048576 when 0 then 'enabled' else 'disabled' end as InsertTriggerStatus,
object_name(updtrig) as UpdateTriggerName,
case sysstat2 & 4194304  when 0 then 'enabled' else 'disabled' end as UpdateTriggerStatus,
object_name(deltrig) as DeleteTriggerName,
case sysstat2 & 2097152  when 0 then 'enabled' else 'disabled' end as DeleteTriggerStatus
from sysobjects where name='mytable'

Keep in mind that the three flags used above are not documented and could by changed by SAP in the future.

To check this for all tables:

select convert(varchar(30), name) as TableName,
convert(varchar(30), isnull(object_name(instrig), '')) as InsertTriggerName,
case sysstat2 & 1048576 when 0 then 'enabled' else 'disabled' end as InsertTriggerStatus,
convert(varchar(30), isnull(object_name(updtrig), '')) as UpdateTriggerName,
case sysstat2 & 4194304  when 0 then 'enabled' else 'disabled' end as UpdateTriggerStatus,
convert(varchar(30), isnull(object_name(deltrig), '')) as DeleteTriggerName,
case sysstat2 & 2097152  when 0 then 'enabled' else 'disabled' end as DeleteTriggerStatus
from sysobjects where type='U' and (instrig<>0 or updtrig<>0 or deltrig<>0)
order by name

If you use identifiers longer than 30 bytes (which is only possible in ASE 15 and above), you can remove the convert varchar(30).

Leave a Reply

Your email address will not be published. Required fields are marked *