IF UPDATE(Organization_Chi) Update D_Forum_Representative Set Organization_Chi = a.Organization_Chi from inserted a Where Main_Representative_ID=a.Representative_ID and a.other_identity<>'1' delete from D_Forum_Representative where Main_Representative_ID in(select Representative_ID from deleted where other_identity = '1')
create TRIGGER [TRIG_D_Forum_Representative_other] ON dbo.D_Forum_Representative FOR UPDATE AS Begin IF UPDATE(Organization_Chi) Update D_Forum_Representative Set Organization_Chi=a.Organization_Chi from inserted a Where D_Forum_Representative.Main_Representative_ID=a.Representative_ID and other_identity<>'1'
create TRIGGER [TRIG_D_Forum_Representative_other] ON dbo.D_Forum_Representative FOR INSERT AS Update D_Forum_Representative Set Organization_Chi=a.Organization_Chi from inserted a Where D_Forum_Representative.Main_Representative_ID=a.Representative_ID and other_identity<>'1'
CREATE TRIGGER [TRIG_D_Forum_Representative_other] ON dbo.D_Forum_Representative FOR delete as update D_Forum_Representative set Main_Representative_ID=null from deleted a where a.Representative_ID=D_Forum_Representative.Main_Representative_ID
create TRIGGER [TRIG_D_Forum_Representative_other] ON dbo.D_Forum_Representative FOR UPDATE AS Begin IF UPDATE(Organization_Chi) Update D_Forum_Representative Set Organization_Chi=a.Organization_Chi-b.Organization_Chi from inserted a deleted b Where a.Main_Representative_ID=b.Representative_ID and D_Forum_Representative.Main_Representative_ID=a.Representative_ID and other_identity<>'1'
关于删除的触发器如何实现 现在是这样几种情况 when (deleted.other_identity = ‘1‘) then delete from D_Forum_Representative where D_Forum_Representative.main_Representative_id= deleted.representative_id else when (deleted.other_identity <>‘1‘) update from D_Forum_representative set D_Forum_representative.ww = D_Forum_representative.ww - 1
if exists(select * from deleted where other_identity = ‘1') delete from D_Forum_Representative where D_Forum_Representative.main_Representative_id= deleted.representative_id else update from D_Forum_representative set D_Forum_representative.ww = a.ww - 1 from deleted a D_Forum_Representative.main_Representative_id= a.representative_id
或者 delete from D_Forum_Representative where D_Forum_Representative.main_Representative_id= deleted.representative_id and deleted.other_identity = ‘1' update from D_Forum_representative set D_Forum_representative.ww = a.ww - 1 from deleted a D_Forum_Representative.main_Representative_id= a.representative_id and .other_identity <>‘1'
以上有错,在from deleted a加 where update from D_Forum_representative set D_Forum_representative.ww = a.ww - 1 from deleted a where D_Forum_Representative.main_Representative_id= a.representative_id and .other_identity <>‘1'
CREATE TRIGGER [TRIG_D_Forum_Representative_other] ON dbo.D_Forum_Representative FOR delete asdelete D_Forum_Representative where main_Representative_id in (select representative_id from deleted where ther_identity <>'1') update D_Forum_representative set D_Forum_representative.ww=D_Forum_representative.ww - 1 from deleted a where a.representative_id=D_Forum_representative.representative_id and a.other_identity<>'1'
Update D_Forum_Representative Set
Organization_Chi = a.Organization_Chi
from inserted a
Where Main_Representative_ID=a.Representative_ID and a.other_identity<>'1'
delete from D_Forum_Representative
where Main_Representative_ID in(select Representative_ID from deleted where other_identity = '1')
FOR UPDATE
AS
Begin
IF UPDATE(Organization_Chi)
Update D_Forum_Representative Set Organization_Chi=a.Organization_Chi from inserted a Where D_Forum_Representative.Main_Representative_ID=a.Representative_ID and other_identity<>'1'
FOR INSERT
AS
Update D_Forum_Representative Set Organization_Chi=a.Organization_Chi from inserted a Where D_Forum_Representative.Main_Representative_ID=a.Representative_ID and other_identity<>'1'
FOR delete
as
update D_Forum_Representative set Main_Representative_ID=null from deleted a where a.Representative_ID=D_Forum_Representative.Main_Representative_ID
FOR UPDATE
AS
Begin
IF UPDATE(Organization_Chi)
Update D_Forum_Representative Set Organization_Chi=a.Organization_Chi-b.Organization_Chi from inserted a
deleted b Where a.Main_Representative_ID=b.Representative_ID and D_Forum_Representative.Main_Representative_ID=a.Representative_ID and other_identity<>'1'
现在是这样几种情况
when (deleted.other_identity = ‘1‘)
then
delete from D_Forum_Representative
where D_Forum_Representative.main_Representative_id=
deleted.representative_id
else when (deleted.other_identity <>‘1‘)
update from D_Forum_representative
set D_Forum_representative.ww = D_Forum_representative.ww - 1
delete from D_Forum_Representative
where D_Forum_Representative.main_Representative_id=
deleted.representative_id
else
update from D_Forum_representative
set D_Forum_representative.ww = a.ww - 1
from deleted a
D_Forum_Representative.main_Representative_id=
a.representative_id
where D_Forum_Representative.main_Representative_id=
deleted.representative_id and deleted.other_identity = ‘1'
update from D_Forum_representative
set D_Forum_representative.ww = a.ww - 1
from deleted a
D_Forum_Representative.main_Representative_id=
a.representative_id and .other_identity <>‘1'
update from D_Forum_representative
set D_Forum_representative.ww = a.ww - 1
from deleted a where
D_Forum_Representative.main_Representative_id=
a.representative_id and .other_identity <>‘1'
FOR delete
asdelete D_Forum_Representative where main_Representative_id in (select representative_id from deleted where ther_identity <>'1')
update D_Forum_representative set D_Forum_representative.ww=D_Forum_representative.ww - 1 from deleted a where a.representative_id=D_Forum_representative.representative_id and a.other_identity<>'1'
将deleted当一个表即可
在UPDATE时DELETED是修改前的记录,INSERTED是修改后的记录应该清楚了