Solved

How to identify what action caused the trigger to fire

Posted on 2003-10-29
6
245 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
6 Comments
 
LVL 8

Expert Comment

by:dishanf
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 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

11 Experts available now in Live!

Get 1:1 Help Now