SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TRIGGERS AFTER UPDATE,DELETE


TRIGGERS AFTER UPDATE,DELETE

Author
Message
john.david.birch
john.david.birch
SSC-Enthusiastic
SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)

Group: General Forum Members
Points: 154 Visits: 31
I have a need for triggers during UPDATE and DELETE. The need in each case is "almost" identical, but there is a slight difference.

Do I have to write two triggers ? or can I introduce some logic in the trigger definition ?

example

Create Trigger dbo.MyCombinedTrigger on dbo.MyTable
...
AFTER UPDATE, DELETE
...

Begin
/*
Common code for "Update" and "Delete" operations
*/

if @@mysteryVariable = DELETE
begin
/* "Delete" specific code */
end


End

Is there such a "@@mysteryVariable" or another technique to do this ?

John
JBZ
JBZ
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 177
John,

The easiest way to find out if this is UPDATE or DELETE event is to check rows in inserted table: for UPDATE event there will be rows in both tables - inserted and deleted, for DELETE event you will find rows only in deleted table.

Is this what you are looking for?

JBZ
john.david.birch
john.david.birch
SSC-Enthusiastic
SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)

Group: General Forum Members
Points: 154 Visits: 31
Yes, thank you very much Smile

Just in case anyone else is wondering. Here is my test code:

create table JDBT1 ( a int, b int);
create table JDBT2 ( i int, d int);
go
create trigger tr1 on JDBT1 after insert, update, delete
as
begin
declare @qI int,@qU int,@qD int;
select @qI = count(*) from inserted;
select @qD = count(*) from deleted;
insert into JDBT2 ( i,d) values (@qI, @qD)
end
go

insert into JDBT1 (a,b) values( 1,2);
update JDBT1 set b=3;
delete from JDBT1;

select * from JDBT2;



which gives a nice little result set of
1, 0
1, 1
0, 1



John
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search