Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Detecting insert, update and delete in an after trigger

 
Goto page 1, 2
   Database Help (Home) -> Programming RSS
Next:  Interesting Dimension challenge - please help!  
Author Message
Andy B

External


Since: Feb 13, 2008
Posts: 215



(Msg. 1) Posted: Mon Nov 10, 2008 7:43 pm
Post subject: Detecting insert, update and delete in an after trigger
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Is there a way to detect insert, delete and update actions in an after
trigger?

 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Eric Isaacs

External


Since: May 13, 2008
Posts: 367



(Msg. 2) Posted: Mon Nov 10, 2008 7:43 pm
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In an after insert/update/delete trigger, all inserts and updates will
be in the Inserted table with the (new) primary key. All the deletes
(and previous values) will be in the Deleted table. If you have
primary keys that don't change (INT IDENTITY or GUID that your
application leaves static all the time) you can generally count on
those PKs to be static for all these operations, so you can determine
inserts where the ID is in the Inserted, but not in the Deleted. You
can determine deletes where the ID is in the Delete table, but not in
the Inserted table, and you can determine updates where the ID is in
the Inserted AND Deleted tables.

If your primary keys can change (because you're using composit PKs or
customer data as your PK), you really can only determine these as
inserts and deletes, as you won't be able to tell when an existing row
has changed if you can't identify it in both the Inserted and Deleted
tables. On the other hand, if there is data in both the Inserted and
Deleted tables for one single execution of the trigger, I suppose you
could make the assumption that all transactions are updates, whereas
if there is only data in Inserted and no data in Deleted, you could
assume they're all inserts, etc. That would probably work too.

-Eric Isaacs

 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Andy B

External


Since: Feb 13, 2008
Posts: 215



(Msg. 3) Posted: Mon Nov 10, 2008 8:55 pm
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Do you have a simple example with comments explaining whats going on?


"Eric Isaacs" wrote in message

> In an after insert/update/delete trigger, all inserts and updates will
> be in the Inserted table with the (new) primary key. All the deletes
> (and previous values) will be in the Deleted table. If you have
> primary keys that don't change (INT IDENTITY or GUID that your
> application leaves static all the time) you can generally count on
> those PKs to be static for all these operations, so you can determine
> inserts where the ID is in the Inserted, but not in the Deleted. You
> can determine deletes where the ID is in the Delete table, but not in
> the Inserted table, and you can determine updates where the ID is in
> the Inserted AND Deleted tables.
>
> If your primary keys can change (because you're using composit PKs or
> customer data as your PK), you really can only determine these as
> inserts and deletes, as you won't be able to tell when an existing row
> has changed if you can't identify it in both the Inserted and Deleted
> tables. On the other hand, if there is data in both the Inserted and
> Deleted tables for one single execution of the trigger, I suppose you
> could make the assumption that all transactions are updates, whereas
> if there is only data in Inserted and no data in Deleted, you could
> assume they're all inserts, etc. That would probably work too.
>
> -Eric Isaacs
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 4) Posted: Tue Nov 11, 2008 2:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Andy
If exists (select * from inserted) and exists (select * from deleted)
'Update happened'
if exists( select * from inserted)
'Insert happened'
else
'Delete happened'




"Andy B" wrote in message

> Do you have a simple example with comments explaining whats going on?
>
>
> "Eric Isaacs" wrote in message
>
>> In an after insert/update/delete trigger, all inserts and updates will
>> be in the Inserted table with the (new) primary key. All the deletes
>> (and previous values) will be in the Deleted table. If you have
>> primary keys that don't change (INT IDENTITY or GUID that your
>> application leaves static all the time) you can generally count on
>> those PKs to be static for all these operations, so you can determine
>> inserts where the ID is in the Inserted, but not in the Deleted. You
>> can determine deletes where the ID is in the Delete table, but not in
>> the Inserted table, and you can determine updates where the ID is in
>> the Inserted AND Deleted tables.
>>
>> If your primary keys can change (because you're using composit PKs or
>> customer data as your PK), you really can only determine these as
>> inserts and deletes, as you won't be able to tell when an existing row
>> has changed if you can't identify it in both the Inserted and Deleted
>> tables. On the other hand, if there is data in both the Inserted and
>> Deleted tables for one single execution of the trigger, I suppose you
>> could make the assumption that all transactions are updates, whereas
>> if there is only data in Inserted and no data in Deleted, you could
>> assume they're all inserts, etc. That would probably work too.
>>
>> -Eric Isaacs
>
>
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Andy B

External


Since: Feb 13, 2008
Posts: 215



(Msg. 5) Posted: Tue Nov 11, 2008 3:38 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Got it. Now, is there a way to make this database wide instead of having to
create a trigger for each table that needs it?


"Uri Dimant" wrote in message

> Andy
> If exists (select * from inserted) and exists (select * from deleted)
> 'Update happened'
> if exists( select * from inserted)
> 'Insert happened'
> else
> 'Delete happened'
>
>
>
>
> "Andy B" wrote in message
>
>> Do you have a simple example with comments explaining whats going on?
>>
>>
>> "Eric Isaacs" wrote in message
>>
>>> In an after insert/update/delete trigger, all inserts and updates will
>>> be in the Inserted table with the (new) primary key. All the deletes
>>> (and previous values) will be in the Deleted table. If you have
>>> primary keys that don't change (INT IDENTITY or GUID that your
>>> application leaves static all the time) you can generally count on
>>> those PKs to be static for all these operations, so you can determine
>>> inserts where the ID is in the Inserted, but not in the Deleted. You
>>> can determine deletes where the ID is in the Delete table, but not in
>>> the Inserted table, and you can determine updates where the ID is in
>>> the Inserted AND Deleted tables.
>>>
>>> If your primary keys can change (because you're using composit PKs or
>>> customer data as your PK), you really can only determine these as
>>> inserts and deletes, as you won't be able to tell when an existing row
>>> has changed if you can't identify it in both the Inserted and Deleted
>>> tables. On the other hand, if there is data in both the Inserted and
>>> Deleted tables for one single execution of the trigger, I suppose you
>>> could make the assumption that all transactions are updates, whereas
>>> if there is only data in Inserted and no data in Deleted, you could
>>> assume they're all inserts, etc. That would probably work too.
>>>
>>> -Eric Isaacs
>>
>>
>
>
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 6) Posted: Tue Nov 11, 2008 5:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Andy
I'm not sure that understood you. You can create three triggers each for DML
opeartion, or much better not using triggers at all, i mean keep all logic
within a stored procedure.



"Andy B" wrote in message

> Got it. Now, is there a way to make this database wide instead of having
> to create a trigger for each table that needs it?
>
>
> "Uri Dimant" wrote in message
>
>> Andy
>> If exists (select * from inserted) and exists (select * from deleted)
>> 'Update happened'
>> if exists( select * from inserted)
>> 'Insert happened'
>> else
>> 'Delete happened'
>>
>>
>>
>>
>> "Andy B" wrote in message
>>
>>> Do you have a simple example with comments explaining whats going on?
>>>
>>>
>>> "Eric Isaacs" wrote in message
>>>
>>>> In an after insert/update/delete trigger, all inserts and updates will
>>>> be in the Inserted table with the (new) primary key. All the deletes
>>>> (and previous values) will be in the Deleted table. If you have
>>>> primary keys that don't change (INT IDENTITY or GUID that your
>>>> application leaves static all the time) you can generally count on
>>>> those PKs to be static for all these operations, so you can determine
>>>> inserts where the ID is in the Inserted, but not in the Deleted. You
>>>> can determine deletes where the ID is in the Delete table, but not in
>>>> the Inserted table, and you can determine updates where the ID is in
>>>> the Inserted AND Deleted tables.
>>>>
>>>> If your primary keys can change (because you're using composit PKs or
>>>> customer data as your PK), you really can only determine these as
>>>> inserts and deletes, as you won't be able to tell when an existing row
>>>> has changed if you can't identify it in both the Inserted and Deleted
>>>> tables. On the other hand, if there is data in both the Inserted and
>>>> Deleted tables for one single execution of the trigger, I suppose you
>>>> could make the assumption that all transactions are updates, whereas
>>>> if there is only data in Inserted and no data in Deleted, you could
>>>> assume they're all inserts, etc. That would probably work too.
>>>>
>>>> -Eric Isaacs
>>>
>>>
>>
>>
>
>
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
TheSQLGuru

External


Since: Jan 11, 2008
Posts: 579



(Msg. 7) Posted: Tue Nov 11, 2008 11:49 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Uri, I think there may have been a logic flaw in your code. Since you don't
have BEGIN/END and an ELSE after the first IF, the second IF will hit for
UPDATES and print 'Insert happened', right?

maybe this (untested)??

If exists (select * from inserted) and exists (select * from deleted)
BEGIN
'Update happened'
END
ELSE
BEGIN
if exists( select * from inserted)
BEGIN
'Insert happened'
END
ELSE
BEGIN
'Delete happened'
END
END


--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"Uri Dimant" wrote in message

> Andy
> If exists (select * from inserted) and exists (select * from deleted)
> 'Update happened'
> if exists( select * from inserted)
> 'Insert happened'
> else
> 'Delete happened'
>
>
>
>
> "Andy B" wrote in message
>
>> Do you have a simple example with comments explaining whats going on?
>>
>>
>> "Eric Isaacs" wrote in message
>>
>>> In an after insert/update/delete trigger, all inserts and updates will
>>> be in the Inserted table with the (new) primary key. All the deletes
>>> (and previous values) will be in the Deleted table. If you have
>>> primary keys that don't change (INT IDENTITY or GUID that your
>>> application leaves static all the time) you can generally count on
>>> those PKs to be static for all these operations, so you can determine
>>> inserts where the ID is in the Inserted, but not in the Deleted. You
>>> can determine deletes where the ID is in the Delete table, but not in
>>> the Inserted table, and you can determine updates where the ID is in
>>> the Inserted AND Deleted tables.
>>>
>>> If your primary keys can change (because you're using composit PKs or
>>> customer data as your PK), you really can only determine these as
>>> inserts and deletes, as you won't be able to tell when an existing row
>>> has changed if you can't identify it in both the Inserted and Deleted
>>> tables. On the other hand, if there is data in both the Inserted and
>>> Deleted tables for one single execution of the trigger, I suppose you
>>> could make the assumption that all transactions are updates, whereas
>>> if there is only data in Inserted and no data in Deleted, you could
>>> assume they're all inserts, etc. That would probably work too.
>>>
>>> -Eric Isaacs
>>
>>
>
>
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 8) Posted: Wed Nov 12, 2008 2:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Kevin
Could be , I have not tested too , but only tried to direct the OP to the
right point




"TheSQLGuru" wrote in message

> Uri, I think there may have been a logic flaw in your code. Since you
> don't have BEGIN/END and an ELSE after the first IF, the second IF will
> hit for UPDATES and print 'Insert happened', right?
>
> maybe this (untested)??
>
> If exists (select * from inserted) and exists (select * from deleted)
> BEGIN
> 'Update happened'
> END
> ELSE
> BEGIN
> if exists( select * from inserted)
> BEGIN
> 'Insert happened'
> END
> ELSE
> BEGIN
> 'Delete happened'
> END
> END
>
>
> --
> Kevin G. Boles
> Indicium Resources, Inc.
> SQL Server MVP
> kgboles a earthlink dt net
>
>
> "Uri Dimant" wrote in message
>
>> Andy
>> If exists (select * from inserted) and exists (select * from deleted)
>> 'Update happened'
>> if exists( select * from inserted)
>> 'Insert happened'
>> else
>> 'Delete happened'
>>
>>
>>
>>
>> "Andy B" wrote in message
>>
>>> Do you have a simple example with comments explaining whats going on?
>>>
>>>
>>> "Eric Isaacs" wrote in message
>>>
>>>> In an after insert/update/delete trigger, all inserts and updates will
>>>> be in the Inserted table with the (new) primary key. All the deletes
>>>> (and previous values) will be in the Deleted table. If you have
>>>> primary keys that don't change (INT IDENTITY or GUID that your
>>>> application leaves static all the time) you can generally count on
>>>> those PKs to be static for all these operations, so you can determine
>>>> inserts where the ID is in the Inserted, but not in the Deleted. You
>>>> can determine deletes where the ID is in the Delete table, but not in
>>>> the Inserted table, and you can determine updates where the ID is in
>>>> the Inserted AND Deleted tables.
>>>>
>>>> If your primary keys can change (because you're using composit PKs or
>>>> customer data as your PK), you really can only determine these as
>>>> inserts and deletes, as you won't be able to tell when an existing row
>>>> has changed if you can't identify it in both the Inserted and Deleted
>>>> tables. On the other hand, if there is data in both the Inserted and
>>>> Deleted tables for one single execution of the trigger, I suppose you
>>>> could make the assumption that all transactions are updates, whereas
>>>> if there is only data in Inserted and no data in Deleted, you could
>>>> assume they're all inserts, etc. That would probably work too.
>>>>
>>>> -Eric Isaacs
>>>
>>>
>>
>>
>
>
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 9) Posted: Wed Nov 12, 2008 3:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Could be , I have not tested too , but only tried to direct the OP to the
> right point

Or should be I wanted point him to the right direction Smile)))



"Uri Dimant" wrote in message

> Kevin
> Could be , I have not tested too , but only tried to direct the OP to the
> right point
>
>
>
>
> "TheSQLGuru" wrote in message
>
>> Uri, I think there may have been a logic flaw in your code. Since you
>> don't have BEGIN/END and an ELSE after the first IF, the second IF will
>> hit for UPDATES and print 'Insert happened', right?
>>
>> maybe this (untested)??
>>
>> If exists (select * from inserted) and exists (select * from deleted)
>> BEGIN
>> 'Update happened'
>> END
>> ELSE
>> BEGIN
>> if exists( select * from inserted)
>> BEGIN
>> 'Insert happened'
>> END
>> ELSE
>> BEGIN
>> 'Delete happened'
>> END
>> END
>>
>>
>> --
>> Kevin G. Boles
>> Indicium Resources, Inc.
>> SQL Server MVP
>> kgboles a earthlink dt net
>>
>>
>> "Uri Dimant" wrote in message
>>
>>> Andy
>>> If exists (select * from inserted) and exists (select * from deleted)
>>> 'Update happened'
>>> if exists( select * from inserted)
>>> 'Insert happened'
>>> else
>>> 'Delete happened'
>>>
>>>
>>>
>>>
>>> "Andy B" wrote in message
>>>
>>>> Do you have a simple example with comments explaining whats going on?
>>>>
>>>>
>>>> "Eric Isaacs" wrote in message
>>>>
>>>>> In an after insert/update/delete trigger, all inserts and updates will
>>>>> be in the Inserted table with the (new) primary key. All the deletes
>>>>> (and previous values) will be in the Deleted table. If you have
>>>>> primary keys that don't change (INT IDENTITY or GUID that your
>>>>> application leaves static all the time) you can generally count on
>>>>> those PKs to be static for all these operations, so you can determine
>>>>> inserts where the ID is in the Inserted, but not in the Deleted. You
>>>>> can determine deletes where the ID is in the Delete table, but not in
>>>>> the Inserted table, and you can determine updates where the ID is in
>>>>> the Inserted AND Deleted tables.
>>>>>
>>>>> If your primary keys can change (because you're using composit PKs or
>>>>> customer data as your PK), you really can only determine these as
>>>>> inserts and deletes, as you won't be able to tell when an existing row
>>>>> has changed if you can't identify it in both the Inserted and Deleted
>>>>> tables. On the other hand, if there is data in both the Inserted and
>>>>> Deleted tables for one single execution of the trigger, I suppose you
>>>>> could make the assumption that all transactions are updates, whereas
>>>>> if there is only data in Inserted and no data in Deleted, you could
>>>>> assume they're all inserts, etc. That would probably work too.
>>>>>
>>>>> -Eric Isaacs
>>>>
>>>>
>>>
>>>
>>
>>
>
>
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 11, 2008
Posts: 440



(Msg. 10) Posted: Wed Nov 12, 2008 5:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 10 Nov 2008 19:43:20 -0500, Andy B wrote:

>Is there a way to detect insert, delete and update actions in an after
>trigger?

Hi Andy,

The easiest way to detect this is to code seperate triggers for each of
the three actions. That way, you always know for sure what operation
fired the trigger!

(The method suggested by Eric and Uri will fail if the trigger fires on
an insert, update, or delete that affects no rows at all - though in
many triggers that won't really matter much...)

>Now, is there a way to make this database wide instead of having to
>create a trigger for each table that needs it?

There is no such way, and frankly, I see very little situations wherre
this would be practical. When a trigger is fired, don't you want it to
do something with the actual changes that caused it to fire? So that
would mean handling different columns in different triggers - far easier
and more efficient to have a trigger per table than to have some generic
code that would require hefty dynamic SQL generation and execution to do
something sensible.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 11) Posted: Wed Nov 12, 2008 5:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Hugo
> (The method suggested by Eric and Uri will fail if the trigger fires on
> an insert, update, or delete that affects no rows at all - though in
> many triggers that won't really matter much...)

What did you mean?
create table t ( c int)

insert into t select 1

create trigger tt on t for update

as

select * from

(select * from inserted

union all

select * from deleted) as d



update t set c=1







"Hugo Kornelis" wrote in message

> On Mon, 10 Nov 2008 19:43:20 -0500, Andy B wrote:
>
>>Is there a way to detect insert, delete and update actions in an after
>>trigger?
>
> Hi Andy,
>
> The easiest way to detect this is to code seperate triggers for each of
> the three actions. That way, you always know for sure what operation
> fired the trigger!
>
> (The method suggested by Eric and Uri will fail if the trigger fires on
> an insert, update, or delete that affects no rows at all - though in
> many triggers that won't really matter much...)
>
>>Now, is there a way to make this database wide instead of having to
>>create a trigger for each table that needs it?
>
> There is no such way, and frankly, I see very little situations wherre
> this would be practical. When a trigger is fired, don't you want it to
> do something with the actual changes that caused it to fire? So that
> would mean handling different columns in different triggers - far easier
> and more efficient to have a trigger per table than to have some generic
> code that would require hefty dynamic SQL generation and execution to do
> something sensible.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 11, 2008
Posts: 440



(Msg. 12) Posted: Wed Nov 12, 2008 6:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 12 Nov 2008 12:11:15 +0200, Uri Dimant wrote:

>Hi Hugo
>> (The method suggested by Eric and Uri will fail if the trigger fires on
>> an insert, update, or delete that affects no rows at all - though in
>> many triggers that won't really matter much...)
>
>What did you mean?

Hi Uri,

What I meant is that any DML action that affects no rows (e.g. UPDATE
.... WHERE 1 = 2) will cause the trigger to fire with no rows in inserted
and no rows in deleted.

CREATE TABLE Test (a int PRIMARY KEY, b int);
go
CREATE TRIGGER TestTrigger
ON Test AFTER INSERT, UPDATE, DELETE
AS
IF EXISTS (SELECT * FROM inserted)
AND EXISTS (SELECT * FROM deleted)
PRINT 'Update happened';
ELSE
IF EXISTS (SELECT * FROM inserted)
PRINT 'Insert happened';
ELSE
PRINT 'Delete happened';
go
UPDATE Test SET b = 2;
go
DROP TABLE Test;
go

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Andy B

External


Since: Feb 13, 2008
Posts: 215



(Msg. 13) Posted: Wed Nov 12, 2008 7:24 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Makes sense...


"Hugo Kornelis" wrote in message

> On Mon, 10 Nov 2008 19:43:20 -0500, Andy B wrote:
>
>>Is there a way to detect insert, delete and update actions in an after
>>trigger?
>
> Hi Andy,
>
> The easiest way to detect this is to code seperate triggers for each of
> the three actions. That way, you always know for sure what operation
> fired the trigger!
>
> (The method suggested by Eric and Uri will fail if the trigger fires on
> an insert, update, or delete that affects no rows at all - though in
> many triggers that won't really matter much...)
>
>>Now, is there a way to make this database wide instead of having to
>>create a trigger for each table that needs it?
>
> There is no such way, and frankly, I see very little situations wherre
> this would be practical. When a trigger is fired, don't you want it to
> do something with the actual changes that caused it to fire? So that
> would mean handling different columns in different triggers - far easier
> and more efficient to have a trigger per table than to have some generic
> code that would require hefty dynamic SQL generation and execution to do
> something sensible.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 14) Posted: Wed Nov 12, 2008 7:25 am
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Hugo

Ok, it is most likely IF UPDATE(col) will fire even col was really updated
Smile)



"Hugo Kornelis" wrote in message

> On Wed, 12 Nov 2008 12:11:15 +0200, Uri Dimant wrote:
>
>>Hi Hugo
>>> (The method suggested by Eric and Uri will fail if the trigger fires on
>>> an insert, update, or delete that affects no rows at all - though in
>>> many triggers that won't really matter much...)
>>
>>What did you mean?
>
> Hi Uri,
>
> What I meant is that any DML action that affects no rows (e.g. UPDATE
> ... WHERE 1 = 2) will cause the trigger to fire with no rows in inserted
> and no rows in deleted.
>
> CREATE TABLE Test (a int PRIMARY KEY, b int);
> go
> CREATE TRIGGER TestTrigger
> ON Test AFTER INSERT, UPDATE, DELETE
> AS
> IF EXISTS (SELECT * FROM inserted)
> AND EXISTS (SELECT * FROM deleted)
> PRINT 'Update happened';
> ELSE
> IF EXISTS (SELECT * FROM inserted)
> PRINT 'Insert happened';
> ELSE
> PRINT 'Delete happened';
> go
> UPDATE Test SET b = 2;
> go
> DROP TABLE Test;
> go
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Eric Isaacs

External


Since: May 13, 2008
Posts: 367



(Msg. 15) Posted: Wed Nov 12, 2008 1:06 pm
Post subject: Re: Detecting insert, update and delete in an after trigger [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

My suggestion was that you check counts in Inserted and Deleted. If
no rows are actually updated, then there won't be any data in those
tables, even if the trigger fires. I think you make a valid point,
however, that Uri's IF block would return that rows were deleted even
if NO rows were deleted or even updated. You would also need to check
the Deleted table.

The one other case to consider is if the rows are updated to their
existing values (the data is updated, but no changes are actually
made.)

USE tempdb

CREATE TABLE Test (a int PRIMARY KEY, b int);
go

INSERT Test (a, b) SELECT 1, 2

GO

CREATE TRIGGER TestTrigger
ON Test AFTER INSERT, UPDATE, DELETE
AS
IF EXISTS (SELECT * FROM inserted)
AND EXISTS (SELECT * FROM deleted)
PRINT 'Update happened';
ELSE
IF EXISTS (SELECT * FROM inserted)
PRINT 'Insert happened';
ELSE IF EXISTS (SELECT * FROM Deleted) --Uri's code also needs this
additional check in the case that nothing is updated.
PRINT 'Delete happened';
go
UPDATE Test SET b = 2;
go
DROP TABLE Test;
go

results in:


(1 row(s) affected)
Update happened

(1 row(s) affected)


....In this case, technically an update happened, but physically
nothing was updated. (The value 2 was updated to 2.) If you need to
know that level of detail, you'll need to have static primary keys or
other static unique values (that you don't update) and you'll need to
compare the inserted and deleted values in each column.

-Eric Isaacs
 >> Stay informed about: Detecting insert, update and delete in an after trigger 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
update insert delete trigger - how to determine which acti.. - In an update, insert, delete trigger how do I deterimine which action caused the trigger. Is the best way to do it by checking the row count in the deleted and inserted tables, as shown below? if (select count(*) from inserted) <> 0 and (select c...

trigger to update a LastModified column for INSERT and UPD.. - I have a LastModified (timestamp) column on every table in the database. I need to create a trigger that will update this column for the row being inserted/updated. How would I do that?

Insert/Update Trigger help - What am I missing in this trigger? Purpose is that when a user insert/update data in table 'newdata'. It should look for the max date in 'olddata'. If date entered is less or equal to max date in olddata, it should not commit transaction. If date..

Trigger for UPDATE, INSERT - If I have a trigger FOR INSERT, UPDATE how do I know which it is when the trigger is running?

Update insert trigger - I have the following trigger code, but I don't know why it has two syntax errors. Please help me to catch the bugs. I would like to update a [char] field in tbl2 based on a change in a [bit] field in tbl1. +++++++++++++++++++++++++++++++++++ CREATE..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Goto page 1, 2
Page 1 of 2

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]