How to distinguish operation type in trigger?

How to distinguish operation type in trigger?

am 31.05.2007 12:19:11 von hubert.trzewik

Hi,

I want to have all-in-one trigger, defined like this:

CREATE TRIGGER MyInsertDeleteUpdateHandler
ON MyTable
FOR DELETE, INSERT, UPDATE
AS
BEGIN
(...)
END

Now, how can I tell why this trigger was fired (what event caused
trigger to be fired) - was it DELETE, INSERT or UPDATE?

Is there something like this: @@event_type,
so I could do for example IF (@@event_type = DELETE) (...)

Of course I can create 3 triggers instead of 1, to be sure what event
fired my trigger.

I can also count records in _deleted_, _inserted_ tables or to do
JOINs with it. But, _inserted_ table is common for UPDATE and INSERT
events..

Any suggestions?
Thanks in advance.

Hubert

Re: How to distinguish operation type in trigger?

am 31.05.2007 13:43:43 von Dan Guzman

> I can also count records in _deleted_, _inserted_ tables or to do
> JOINs with it. But, _inserted_ table is common for UPDATE and INSERT
> events..

However, the deleted table will be empty for when fired by INSERT. You can
determine the statement type as follows:

IF EXISTS(SELECT * FROM inserted)
IF EXISTS(SELECT * FROM deleted)
SELECT @event_type = 'update'
ELSE
SELECT @event_type = 'insert'
ELSE
IF EXISTS(SELECT * FROM deleted)
SELECT @event_type = 'delete'
ELSE
--no rows affected - cannot determine event
SELECT @event_type = 'unknown'

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Hubert Trzewik" wrote in message
news:1180606751.757434.101410@m36g2000hse.googlegroups.com.. .
> Hi,
>
> I want to have all-in-one trigger, defined like this:
>
> CREATE TRIGGER MyInsertDeleteUpdateHandler
> ON MyTable
> FOR DELETE, INSERT, UPDATE
> AS
> BEGIN
> (...)
> END
>
> Now, how can I tell why this trigger was fired (what event caused
> trigger to be fired) - was it DELETE, INSERT or UPDATE?
>
> Is there something like this: @@event_type,
> so I could do for example IF (@@event_type = DELETE) (...)
>
> Of course I can create 3 triggers instead of 1, to be sure what event
> fired my trigger.
>
> I can also count records in _deleted_, _inserted_ tables or to do
> JOINs with it. But, _inserted_ table is common for UPDATE and INSERT
> events..
>
> Any suggestions?
> Thanks in advance.
>
> Hubert
>

Re: How to distinguish operation type in trigger?

am 31.05.2007 13:48:38 von masri999

On May 31, 3:19 pm, Hubert Trzewik wrote:
> Hi,
>
> I want to have all-in-one trigger, defined like this:
>
> CREATE TRIGGER MyInsertDeleteUpdateHandler
> ON MyTable
> FOR DELETE, INSERT, UPDATE
> AS
> BEGIN
> (...)
> END
>
> Now, how can I tell why this trigger was fired (what event caused
> trigger to be fired) - was it DELETE, INSERT or UPDATE?
>
> Is there something like this: @@event_type,
> so I could do for example IF (@@event_type = DELETE) (...)
>
> Of course I can create 3 triggers instead of 1, to be sure what event
> fired my trigger.
>
> I can also count records in _deleted_, _inserted_ tables or to do
> JOINs with it. But, _inserted_ table is common for UPDATE and INSERT
> events..
>
> Any suggestions?
> Thanks in advance.
>
> Hubert

For Inserted : Rows are in inserted only
For Updated: Rows are in inserted and deleted
For deleted: Rows are in deleted only

Re: How to distinguish operation type in trigger?

am 31.05.2007 15:32:09 von hubert.trzewik

On May 31, 1:48 pm, M A Srinivas wrote:
> On May 31, 3:19 pm, Hubert Trzewik wrote:
>
>
>
> > Hi,
>
> > I want to have all-in-one trigger, defined like this:
>
> > CREATE TRIGGER MyInsertDeleteUpdateHandler
> > ON MyTable
> > FOR DELETE, INSERT, UPDATE
> > AS
> > BEGIN
> > (...)
> > END
>
> > Now, how can I tell why this trigger was fired (what event caused
> > trigger to be fired) - was it DELETE, INSERT or UPDATE?
>
> > Is there something like this: @@event_type,
> > so I could do for example IF (@@event_type = DELETE) (...)
>
> > Of course I can create 3 triggers instead of 1, to be sure what event
> > fired my trigger.
>
> > I can also count records in _deleted_, _inserted_ tables or to do
> > JOINs with it. But, _inserted_ table is common for UPDATE and INSERT
> > events..
>
> > Any suggestions?
> > Thanks in advance.
>
> > Hubert
>
> For Inserted : Rows are in inserted only
> For Updated: Rows are in inserted and deleted
> For deleted: Rows are in deleted only

That's right. One test more and.. UPDATE is seen both in _inserted_
and _deleted_, so I can distinguish it from INSERT (and DELETE of
course).

Thanks a lot.

Hubert

Re: How to distinguish operation type in trigger?

am 04.06.2007 20:48:25 von Seribus Dragon

Why not just break up the action into three triggers? if there is
common code use a stored procedure got that part.

Hubert Trzewik wrote:
> Hi,
>
> I want to have all-in-one trigger, defined like this:
>
> CREATE TRIGGER MyInsertDeleteUpdateHandler
> ON MyTable
> FOR DELETE, INSERT, UPDATE
> AS
> BEGIN
> (...)
> END
>
> Now, how can I tell why this trigger was fired (what event caused
> trigger to be fired) - was it DELETE, INSERT or UPDATE?
>
> Is there something like this: @@event_type,
> so I could do for example IF (@@event_type = DELETE) (...)
>
> Of course I can create 3 triggers instead of 1, to be sure what event
> fired my trigger.
>
> I can also count records in _deleted_, _inserted_ tables or to do
> JOINs with it. But, _inserted_ table is common for UPDATE and INSERT
> events..
>
> Any suggestions?
> Thanks in advance.
>
> Hubert
>

Re: How to distinguish operation type in trigger?

am 04.06.2007 23:15:44 von Erland Sommarskog

Seribus Dragon (Seribus.news@seribus.com) writes:
> Why not just break up the action into three triggers? if there is
> common code use a stored procedure got that part.

That can be problematic, if you need to refer to the inserted/deleted
tables, which you often do in triggers.

Most of my triggers are for INSERT and UPDATE, but I have a couple
that are for all three actions.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx