[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Trigger mode

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?
0
sifuhall
Asked:
sifuhall
  • 2
  • 2
1 Solution
 
danblakeCommented:
Yes, have a look at my web-site I demonstrate some code to do this, and give the template.

www.dsystems.plus.com --> 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.
0
 
muzzy2003Commented:
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'
0
 
danblakeCommented:
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 )  )
BEGIN
            GOTO UPD /*Goto the update section of the trigger*/
END
if exists (select top 1 *  from inserted )  
BEGIN
            GOTO INS /*Goto the insert section of the trigger*/
END
if exists (select top 1 *  from deleted )  
BEGIN
            GOTO DEL /*Goto the delete section of the trigger*/
END

DEL:
/*Delete section of trigger*/
INS:
/*Insert section of trigger*/
UPD:
/*Update section of trigger*/
....
0
 
muzzy2003Commented:
Yes - we have basically the same solution packaged up in a different way.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now