ALTER TRIGGER [dbo].[trg_sinhvien_update_malop]
ON [dbo].[SinhVien]
FOR UPDATE AS
declare @malop nvarchar(4)
declare @malopcu nvarchar(4)
declare @sl int
declare @slcu int
IF UPDATE(MaLop)
select @malop=i.MaLop from inserted i inner join deleted d on i.MaSV=d.MaSV
select @malopcu=d.MaLop from inserted i inner join deleted d on i.MaSV=d.MaSV
select @sl= count(*) from SinhVien where MaLop=@malop
select @slcu= count(*) from SinhVien where MaLop=@malopcu
UPDATE Lop
SET SoLuongSV=@sl
where MaLop=@malop
UPDATE Lop
SET SoLuongSV=@slcu
where MaLop=@malopcu
select *
from Lop
Update SinhVien
set MaLop='L1'
where MaSV='SV01'