首页 > 代码库 > 触发器
触发器
触发器---特殊的存储过程,通过增减改数据来自动引发
create trigger TR_student_insert (TR_表明_操作)
on student
for insert(for 是新增之后触发)
as
begin
end
go
----------------------------------------------------------
alter trigger TR_student_insert
on student
for insert
as
begin
update student set sname=‘大混蛋‘ where sname=‘王八蛋‘
end
go
insert into Student values (304,‘王八蛋‘,‘男‘,‘1990-12-14‘,‘95031‘)
insert into Student values(306,‘大混蛋‘,‘男‘,‘1992-09-02‘,‘95035‘)
select *from Student
alter trigger TR_student_delete
on student
instead of delete
as
begin
declare @sno int
select @sno=sno from deleted
delete from score where sno=@sno
delete from student where sno=@sno
end
go
delete from Student where Sno=304
select *from Student insert into Score values(304,‘3-105‘,98)
select *from Score
create trigger TR_student_inserted
on student
instead of insert
as
begin
declare @sno int,@sname varchar(20),@ssex varchar(20),@sbirthday datetime,@class varchar(20)
select @sno=sno,@sname=sname,@ssex=ssex,@sbirthday=sbirthday,@class=class from inserted
if @ssex=‘男‘
begin
set @ssex=‘1‘
end
insert into student values (@sno,@sname,@ssex,@sbirthday,@class)
end
go
insert into Student values(303,‘李白‘,‘男‘,‘1992-03-12‘,‘95033‘)
select *from Student
inserted---临时表 ,获取数据 ,此题中 inserted 代替触发器外的insert。
---触发器中的insert是把执行后的inserted 插入到student表中
触发器