create table scount ( student_count int ) go create table s ( id int, name nvarchar(20) ) go create trigger tri on s for insert,delete as declare @iCount int declare @dCount int
select @iCount=count(*) from inserted select @dCount=count(*) from deleted if @iCount>0 update scount set student_count=student_count+@iCount else if @dCount>0 update scount set student_count=student_count-@dCount goinsert scount values(0)insert s values(1,'张三') insert s values(2,'李四') insert s values(3,'王五')select * from scountdelete s from s where id=1select * from scountdrop table scount drop table s
(
student_count int
)
go
create table s
(
id int,
name nvarchar(20)
)
go
create trigger tri on s
for insert,delete
as
declare @iCount int
declare @dCount int
select @iCount=count(*) from inserted
select @dCount=count(*) from deleted if @iCount>0
update scount set student_count=student_count+@iCount
else if @dCount>0
update scount set student_count=student_count-@dCount
goinsert scount values(0)insert s values(1,'张三')
insert s values(2,'李四')
insert s values(3,'王五')select * from scountdelete s from s where id=1select * from scountdrop table scount
drop table s