create trigger tr_tb
on tb
for insert,update,delete
asupdate ta
set [count]=(select count(*) from tb where taid=ta.id)
from (select taid from inserted union select taid from deleted) as t
where ta.id=t.taid
go--注意:这个触发器效率不怎么样
on tb
for insert,update,delete
asupdate ta
set [count]=(select count(*) from tb where taid=ta.id)
from (select taid from inserted union select taid from deleted) as t
where ta.id=t.taid
go--注意:这个触发器效率不怎么样
for insert,update,delete
as
update ta set [count]=[count]+t.cnt from
(select isnull(a.id,b.id) id,isnull(a.cnt,0)-isnull(b.cnt,0) cnt
from
(select id,count(*) cnt from inserted group by id) a full join
(select id,count(*) cnt from deleted group by id) b
on a.id=b.id) t
where ta.id=t.id
go