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?
LVL 1
sifuhallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.