Trigger mode

Posted on 2004-11-18
Last Modified: 2012-05-05
When inside a trigger is there any way to determine what mode the trigger is using (insert, update, or delete)?

I know that if you have access to the inserted table it must be either an insert or an update.

Likewise, if you have access to the deleted table it must be either a delete or an update.

But is there a method to determine exactly what operation the trigger is performing?
Question by:sifuhall
    LVL 13

    Expert Comment

    Yes, have a look at my web-site I demonstrate some code to do this, and give the template. --> Look under Links -> Articles, Trigger examples.
    The Trigger Code gives examples of both single/multi row updates within the trigger for firing off various dependancys as rqd.
    LVL 16

    Accepted Solution

    danblake - can't find an article called "Trigger examples".

    sifuhall - you answered your own question. This (or similar) is the only way:

    DECLARE @inserts int, @deletes int, @mode char(6)

    SELECT @inserts = count(*) from inserted
    SELECT @deletes = count(*) from deleted

    IF @inserts > 0 AND @deletes > 0
       SET @mode = 'UPDATE'
    IF @inserts > 0 AND @deletes = 0
       SET @mode = 'INSERT'
    IF @inserts = 0 AND @deletes > 0
       SET @mode = 'DELETE'
    LVL 13

    Expert Comment

    The exact title is Article: DDL_Trigger_Migration_Updates

    this gives the example within Appendix B, as follows:
    if (exists (select top 1 *  from inserted ) and
    exists (select top 1 *  from deleted )  )
                GOTO UPD /*Goto the update section of the trigger*/
    if exists (select top 1 *  from inserted )  
                GOTO INS /*Goto the insert section of the trigger*/
    if exists (select top 1 *  from deleted )  
                GOTO DEL /*Goto the delete section of the trigger*/

    /*Delete section of trigger*/
    /*Insert section of trigger*/
    /*Update section of trigger*/
    LVL 16

    Expert Comment

    Yes - we have basically the same solution packaged up in a different way.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now