Solved

How to identify what action caused the trigger to fire

Posted on 2003-10-29
6
277 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

756 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