Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

"instead of delete" trigger is not fired from "after" trigger Expand / Collapse
Author
Message
Posted Wednesday, November 21, 2012 12:05 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, September 28, 2014 11:07 PM
Points: 50, Visits: 125
Hi everyone,

I have a problem that "instead of delete" trigger is not fired from "after insert" trigger. Reading BOL it seems that "after" triggers
should fire "instead of" triggers. I have nested triggers option set to 1, recursive triggers option is on. Below is an example. The example is
very simplified (no constraints, no real life logic) and I use it just to demonstrate the problem. Table t1 has 2 triggers - instead of delete trigger just
writes to t1History table. Second trigger calls delete from t1 if the same row as valid inserted already exists with status 0.
You can see that when test statement1 is called, history row is not created even though the old row is deleted.

Can you please help me understand why the "instead of delete" trigger is not fired from "after" trigger? I have SQL Server 2008 R2.

select SERVERPROPERTY('ProductVersion'), SERVERPROPERTY('ProductLevel'), SERVERPROPERTY('Edition')
10.50.2500.0 SP1 Enterprise Edition (64-bit)

Thank you,
EKD

create table t1History(
itemCode int,
itemName varchar(200),
eventdate smalldatetime

)

create table t1(
itemCode int,
itemName varchar(200),
itemStatus int --0- old, 1- new
)

insert into t1 (itemCode, itemName, itemStatus) values (1, '111', 0)
insert into t1 (itemCode, itemName, itemStatus) values (1, '222', 1)
go
create trigger t1AfterInsert on t1
for insert
as
delete From t1
where exists (Select 1 from inserted where inserted.itemName = t1.itemName and inserted.itemStatus = 1 and t1.itemStatus = 0)
go
create trigger t1InsteadOfDelete on t1
instead of delete
as
insert into t1History
select itemCode, itemName, GETDATE() from deleted

delete from t1
from t1
join deleted d on d.itemCode = t1.itemCode and d.itemName = t1.itemName and d.itemStatus = t1.itemStatus
go

--test statement1
---- the below statement calls after insert trigger and teh row with status 0 is successfully deleted, but no row in t1History is created!
insert into t1 (itemCode, itemName, itemStatus) values (1, '111', 1)

--test statement2
--the below statement correctly fires instead of delete trigger
delete From t1 where itemName = '222'

Post #1387252
Posted Thursday, March 14, 2013 2:17 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:14 PM
Points: 7,125, Visits: 12,719
This thread seems to have gotten to the bottom of the behavior you are seeing:

http://www.sqlservercentral.com/Forums/Topic1430725-391-1.aspx


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1431256
Posted Thursday, March 14, 2013 7:38 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, September 28, 2014 11:07 PM
Points: 50, Visits: 125
Thank you for the link!
Post #1431321
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse