不能通过exec来调用而是通过增删改来调用它,实际上还是一个存储过程,引发和关闭
--create trigger 起名(tr_哪个表名_增删改,一般是这样买、方便记忆)neng帮助你将关联的数据删除掉
--on 在那个表上
--for 为何而建,删除之后在执行
alter trigger tr_student_delete
--create trigger tr_student_delete
on student
--instead of delete--当删除时先引发触发器,当触发器里的操作替换本来的操作
--after delete
for delete
as--as和go中可以写任何代码
--delete from score where sno=109
insert into student values('109','王此人','女','1976-2-3','95031')
--delete from student where sno=109
go
delete from student where sno=109 --删除不掉,语句已终止,而触发器能删除相关联的表的数据
select * from student
select * from score
--for 是先执行as go 下面的,再执行as go 里面的
--instead of 只执行as go这里面的
--,与外面的go后面的没有关系,go后面的可以是增删改,但都不会对数据造成影响,它只起触发as go中的程序的作用
alter trigger dongtai
on teacher
instead of delete--写上了一个删除的
as
begin
declare @tno varchar(20)
set @tno=(select tno from deleted )--deleted临时表,也可以用inserted
update teacher set tname='路虎' where tno=@tno
end
go
delete from teacher where tno='831'
select * from teacher
create trigger tr_teacher_insert
on teacher
for insert
as
begin
declare @tno varchar(20)
set @tno=(select tno from inserted)
end
go
insert into teacher values ('900','胡汉','男','1990-9-2','教授','计算机系')
alter trigger tr_teacher1_insert--触发器可以操作视图,可以再视图上间触发器
on teacher
for insert
as
begin
declare @tno varchar(20)
set @tno=(select tno from inserted)
declare @sex varchar(10)
set @sex =(select tsex from teacher where tno =@tno)
if @sex='男'
update teacher set tsex='女' where tno=@tno
else
update teacher set tsex='男' where tno=@tno
end
go
insert into teacher values('805','找刘','男','1990-9-2','教授','计算机系')
select * from teacher
--忘了名字可以全部禁用
--alter table 在哪个表上建的触发器就是哪个表 disable trigger all--
--alter table 在哪个表上建的触发器就是哪个表 enable trigger all--all是在忘记名字的情况下,开启所有的触发器
exec sp_addtype hei,'decimal(12,3)','not null'
exec sp_droptype hei