create or replace trigger test_trigger
after update on 学生表
for each row
begin
update 班级表 set 班级表.人数=班级表.人数-1 where 班级表.班级= :old.班级;
update 班级表 set 班级表.人数=班级表.人数+1 where 班级表.班级= :new.班级;
commit;
end;
if (object_id('tri','tr')is not null )
drop trigger tri
go
create trigger tri
ON dbo.学生表
FOR UPDATE
as
IF UPDATE(班级id)
declare @班级1 int
declare @班级2 int
SELECT @班级1=班级id
from inserted
SELECT @班级2=班级id
FROM deleted
update 班级表 set 人数=人数+1 where 班级= @班级1
update 班级表 set 人数=人数-1 where 班级= @班级2
GO