Home Deleting and Altering Triggers
Post
Cancel

Deleting and Altering Triggers

  • Deleting Table and View Triggers

    1
    
          DROP TRIGGER PreventNewDiscounts;
    
  • Deleting Database Triggers

    1
    2
    3
    4
    
          DROP TRIGGER PreventNewDiscounts;
    
          DROP TRIGGER PreventViewsModifications
          ON DATABASE:
    
  • Deleting Server Triggers

    1
    2
    3
    4
    5
    
          DROP TRIGGER PreventNewDiscounts;
    
          DROP TRIGGER PreventViewsModifications ON DATABASE:
    
          DROP TRIGGER DisallowLinkedServers ON ALL SERVER;
    
  • Disabling Triggers

    1
    2
    3
    4
    5
    6
    7
    8
    
          DISABLE TRIGGER PreventNewDiscounts
          ON Discounts;
    
          DISABLE TRIGGER PreventViewsModifications
          ON DATABASE;
    
          DISABLE TRIGGER DisallowLinkedServers
          ON ALL SERVER;
    
  • Enabling Triggers

    1
    2
    3
    4
    5
    6
    7
    8
    
          ENABLE TRIGGER PreventNewDiscounts
          ON Discounts;
    
          ENABLE TRIGGER PreventViewsModifications
          ON DATABASE;
    
          ENABLE TRIGGER DisallowLinkedServers
          ON ALL SERVER;
    
  • Altering Triggers

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
          CREATE TRIGGER PreventDiscountsDelete
          ON Discounts
          INSTEAD OF DELETE
          AS  
              PRINT 'You are not allowed to data from the Discounts table.';
    
          DROP TRIGGER PrevetnDiscountsDelete;
            
    
          CREATE TRIGGER PreventDiscountsDelete
          ON Discounts
          INSTEAD OF DELETE
          AS
              PRINT "You are note allowed to remove data from the Discounts table."
    
          ALTER TRIGGER PreventDiscountsDelete
          ON Discounts
          INSTEAD OF DELETE
          AS 
              PRINT "You are not allowed to remove data from the Discounts table.';
    

Trigger Management

  • Getting info from sys.triggers

    1
    
          SELECT * FROM sys.triggers;
    

    image

    nametrigger name
    object_idunique identifier of the trigger
    parent_classtrigger type as integer
    parent_class_desctrigger type as text
    parent_idunique identifier of the parent object
    create_datedate of creation
    modify_datedate of last modification
    is_disabledcurrent state
    is_instead_of_triggerINSTEAD OF or AFTER trigger
  • Getting info from sys.server_triggers

    1
    
          SELECT * FROM sys.server_triggers;
    
  • Getting info from sys.trigger_events

    1
    
          SELECT * FROM sys.trigger_events;
    

    image

    object_idunique identifier of the trigger
    typeevent type as integer
    type_descevent type as text
    event_group_typeevent group type as integer
    event_group_type_descevent group type as text
  • Getting info from sys.server_trigger_events

    1
    
          SELECT * FROM sys.server_trigger_events;
    

Troubleshooting triggers

  • Tracking Trigger Executinos (system view)

    1
    
          SELECT * FROM sys.dm_exec_trigger_stats;
    
  • Tracking Trigger Executions (custom solution)

    1
    2
    3
    4
    5
    
          CREATE TRIGGER PreventOrdersUpdate
          ON Orders
          INSTEAD OF UPDATE
          AS 
              RAISERROR('Updates on "Orders" table are not permitted. Place a new order to add new products.', 16, 1);
    
  • Identifying triggers attached to a table

    1
    2
    3
    4
    
          SELECT name AS TableName,
                  object_id AS TableId
          FROM sys.objects
          WHERE name = "Products";
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
          SELECT o.name AS TableName,
              o.object_id AS TableID,
              t.name AS TriggerName,
              t.object_id AS TriggerID,
              t.is_disabled AS IsDisabled,
              t.is_instead_of_trigger AS IsInsteadOf
          FROM sys.objects AS o
          INNER JOIN sys.triggers AS t ON t.parent_id = o.object_id
          WHERE o.name = 'Products';
    
  • Identifying events capable of firing a trigger

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
              SELECT o.name AS TableName,
                  o.object_id AS TableID,
                  t.name AS TriggerName,
                  t.object_id AS TriggerID,
                  t.is_disabled AS IsDisabled,
                  t.is_instead_of_trigger AS IsInsteadOf,
                  te.type_desc AS FiringEvent
              FROM sys.objects AS o
              INNER JOIN sys.triggers AS t ON t.parent_id = o.object_id
              INNER JOIN sys.trigger_events AS te ON t.object_id = te.object_id
              WHERE o.name = 'Products';
    
  • Viewing the trigger definitions

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
          SELECT o.name AS TableName,
              o.object_id AS TableID,
              t.name AS TriggerName,
              t.object_id AS TriggerID,
              t.is_disabled AS IsDisabled,
              t.is_instead_of_trigger AS IsInsteadOf,
              te.type_desc AS FiringEvent,
              OBJECT_DEFINITION(t.object_id) AS TriggerDefinition
          FROM sys.objects AS o
          INNER JOIN sys.triggers AS t ON t.parent_id = o.object_id
          INNER JOIN sys.trigger_events AS te ON t.object_id = te.object_id
          WHERE o.name = 'Products';
    
This post is licensed under CC BY 4.0 by the author.