日期:2014-05-16  浏览次数:20427 次

触发器(2)

create trigger gra_update

on student?

for delete

as

declare @sname char(10)

select @sname=sno from deleted

if @sname='95001'

begin rollback transaction

print'95001不能被删除'

end

else

begin

?print @sname+'改条目已删除'

end

?

?

alter trigger gra_update

on student?

for delete

as

declare @sname char(10)

select @sname=sno from deleted

if @sname='95001'

begin rollback transaction

print'95001不能被删除'

end

else

begin

?print @sname+'改条目已删除'

end

?

删除触发器的建立]

?

delete from student where sno='95002'

删除1个条目

?

?

?

alter trigger gra_update1

on student

for update,insert,delete

as

declare @sname char(10)

declare @grade smallint

select @sname=sno,@grade=grade from student where grade>100 or grade<0?

if @grade<0

begin ?

update student set grade=0 where sno=@sname

print '成绩有误'+@sname+'成绩为零'

end

else if @grade>100

begin rollback transaction?

print'chengjiyouwu'+@sname+'成绩无效'

end

else?

begin

print'输入成绩有效 已经更新'

end

?

一个触发器 当输入成绩小于0的时候提示错误

当输入成绩大于100时 回滚 修改的成绩不变

?

?

update student set grade=108 where sno='95004'

修改95004的分数 输入108分 但是有触发器的管理

成绩为100 如下:

输入成绩有效 已经更新

?

(所影响的行数为 1 行)

?

95004 ? ? grade over 100

输入成绩有效 已经更新

?

(所影响的行数为 1 行)

?

create或

alter trigger sno_insert

on student

for insert

as

declare @sno char(10)

select @sno=sno from insered?

select @sno=count(sno) from student where sno=@sno

insert into tmp_insered values(@sno)

if @sno>1

begin

rollback transaction

print '数据无效'

end

?