做个触发器吧 CREATE TRIGGER upd_id ON Table2 FOR update ASupdate table1 set c_cmt=c_cmt-1 from table1 a inner join deleted b on a.c_id=b.c_idupdate table1 set c_cmt=c_cmt+1 from table1 a inner join inserted on a.c_id=b.c_id
CREATE TRIGGER upd_id ON Table2 FOR INSERT, UPDATE, DELETE ASupdate table1 a set c_cmt=(select count(s_id) from table2 where id=a.id )go
触发器好啦 CREATE TRIGGER upd_id ON Table2 FOR update ASupdate table1 set a.c_cmt=a.c_cmt-1 from table1 a , deleted b where a.c_id=b.c_idupdate table1 set a.c_cmt=a.c_cmt+1 from table1 a,inserted b where a.c_id=b.c_id
creat trigger upd_id on table2 for insert ,update,delete asTRUNCATE TABLE table1 insert into #table1 select s_id as c_id, count(s_id) as s_id group by c_id
做个触发器 create trigger ... on biao for update update .... set ....=sum(....) where cid = (select cid from inserted)
改一下,前一个写错create trigger t_table2 on table2 for insert,update,delete as update table1 set c_cmt=(select count(s_id) from table2 where c_id=a.c_id ) from table1 a go
总之重新统计所有行
CREATE TRIGGER upd_id
ON Table2
FOR update
ASupdate table1 set c_cmt=c_cmt-1 from
table1 a
inner join deleted b
on a.c_id=b.c_idupdate table1 set c_cmt=c_cmt+1 from
table1 a
inner join inserted
on a.c_id=b.c_id
ON Table2
FOR INSERT, UPDATE, DELETE
ASupdate table1 a set c_cmt=(select count(s_id) from table2 where id=a.id )go
CREATE TRIGGER upd_id
ON Table2
FOR update
ASupdate table1 set a.c_cmt=a.c_cmt-1 from
table1 a , deleted b where a.c_id=b.c_idupdate table1 set a.c_cmt=a.c_cmt+1 from
table1 a,inserted b where a.c_id=b.c_id
on table2
for insert ,update,delete
asTRUNCATE TABLE table1
insert into #table1
select s_id as c_id, count(s_id) as s_id
group by c_id
create trigger ... on biao
for update
update ....
set ....=sum(....)
where cid = (select cid from inserted)
for insert,update,delete
as
update table1 set c_cmt=(select count(s_id) from table2 where c_id=a.c_id )
from table1 a
go