Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to identify what action caused the trigger to fire

Posted on 2003-10-29
6
Medium Priority
?
293 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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 500 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

722 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