Solved

How to identify what action caused the trigger to fire

Posted on 2003-10-29
6
288 Views
Last Modified: 2012-08-13
I have a common trigger that is executed after insert, update and delete. I need to know what action caused the trigger to fire. for example the trigger fired becaused of a row deleted or bcos a row added etc.
0
Comment
Question by:swtirs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 9648145
put this into trigger

INSERT INTO Table1(INSERT)
SELECT COUNT(*)
FROM Inserted

u can alse do this for "deleted" and "updated" trigger tables
0
 
LVL 6

Expert Comment

by:robertjbarker
ID: 9648221
I do not think there is an "updated" table in a trigger.

But if you have a primary key you might be able to use something like the idea.  Choose a column, say pkcol, that is part of the PK, and say it's an int. (I have not tested this)

declare @pkinserted  int
declare @pkdeleted   int
set @pkinserted = pkcol from inserted
set @pkdeleted = pkcol from deleted

if  @pkinserted is null and @pkdeleted is not null
  begin
   -- trigger is from delete
  end

if  @pkinserted is not null and @pkdeleted is not null
  begin
   -- trigger is from update
  end

if  @pkinserted is not null and @pkdeleted is null
  begin
   -- trigger is from insert
  end
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9648546
If it was an insert there will be rows in INSERTED and no rows in DELETED

If it was an update there will be rows in INSERTED and rows in DELETED

If it was a deletion there will be rows in DELETED and no rows in INSERTED.

These are all 'virtual tables' accessible from your trigger.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:ShogunWade
ID: 9649702
If you full outer join the inserted and deleted tables on the primary key you can tell.


EG:

SELECT CASE WHEN d.ID IS NULL THEN 'inserted' WHEN i.ID IS NULL THEN 'deleted' ELSE 'updated' END TypeOfOperation
FROM INSERTED i
   FULL OUTER JOIN DELETED d ON i.ID=d.ID
0
 
LVL 15

Accepted Solution

by:
namasi_navaretnam earned 125 total points
ID: 9650564
If Exists(Select 1 from Inserted)
BEGIN
   
   If Exists (Select 1 from Deleted)
   BEGIN
      // This is Update Trigger
   END
   ELSE
   BEGIN
      //THis is INSERT TRIGGER
   END
END
ELSE
BEGIN
   IF EXISTS(SELECT 1 FROM Deleted)
   BEGIN
       // THIS IS DELETE TRIGGER
   END
END
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9651419
Perhaps, I am being stupid but.  Having mulled over this question all afternoon,  I am still biemused as to this question..........

Why do you need to know what operation caused the trigger to fire?

Why dont you simply have an INSERT trigger and UPDATE trigger and a DELETE trigger?
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

630 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