有三种建立触发器的方式写法,我们来看一下哪种正确
1.第一种方式
create trigger T
on stucou
for insert,update,delete
as
begin
update course set willnum=willnum+(select count(couno)from inserted) where couno=(select couno from inserted)
update course set willnum=willnum-(select count(couno)from deleted) where couno=(select couno from deleted)
end
注意此时的couno表中的willnum
删除stucou表中的数据
此时的couno表中的willnum为
故这种触发器正确被建立
2.第二种方式建立触发器
go
create trigger T
on stucou
for insert,update,delete
as
begin
update course set willnum=willnum+1 where couno=(select couno from inserted)
update course set willnum=willnum-1 where couno=(select couno from deleted)
end
批量删除stucou中的couno值为017的数据
故这种方式也是可行的
3.第三种建立触发器的方式
go
create trigger T
on stucou
for insert,update,delete
as
begin
update course set willnum=willnum+1 where couno=(select couno from inserted)
update course set willnum=willnum-1 where couno=(select couno from deleted)
rollback transaction
end
删除stucou表中的couno值为018的数据
此时会报错的
故这种方式建立触发器是行不通的。