本文已参与「新人创作礼」活动,一起开启掘金创作之路
1.什么是触发器
触发器是一种特殊类型的存储过程,它在指定的表中的数据发生变化时自动生效。唤醒调用触发器以响
应 INSERT、UPDATE 或 DELETE 语句。触发器可以查询其它表,并可以包含复杂的Transact-SQL语句。
将触发器和触发它的语句作为可在触发器内回滚的单个事务对待。如果检测到严重错误(例如,磁盘空
间不足),则整个事务即自动回滚。
2.触发器和约束
触发器可以支持约束的所有功能;但它在所给出的功能上并不总是最好的方法。
3.使用触发器一定要清楚两个虚似表:
1:inserted,事务中被插入的所有记录 2:deleted,事务中被删除的所有记录。
4.触发器管理
1:利用sp_helptrigger命令来查看一个表关联的触发器。sp_helptrigger 表名
2:利用sp_helptext命令来查询某一个触发器的代码。sp_helptest 触发器名。
3:删除触发器,和删除数据库其它对象类似,drop trigger 触发器名。
4:修改触发器和生成触发器内容差不多,只不过修改的语句为alter trigger。
5:利用exec sp_rename命令来完成触发器的重命名。
5.使用
1:如果能够使用约束实现的功能,就不要使用触发器。
2:如果可以使用存储过程来完成,那么也不分使用触发器。
--insert插入触发器
if exists(select * from sysobjects where name='testtri')
drop trigger testtri
go
create trigger testtri
on stuInfo
for insert--(insert\delete\update)--after=for brfore操作前触发
as
declare @stuId nvarchar(50)
declare @stuSeate int
-- declare @errorSum int --错误号
--set @errorSum=0
--从inserted表中查询数据给变量赋值
select @stuId=stuId,@stuSeate=stuSeate from inserted
--根据交易类型更新帐户表中的数据
if(@stuSeate=10)
begin
insert into stuMarks(testID,stuID,writeGrade,computerGrade) values ('E201101005',@stuId,100,100)
end
else
begin
insert into stuMarks(testID,stuID,writeGrade,computerGrade) values ('E201101006',@stuId,0,0)
end
go
--插入方法一 只能执行前一句
insert into stuInfo(stuId,stuName,stuSex,stuAge,stuEamil,stuSeate)
values('bj201105','唐伯虎','男',11,'tbh@163.com',10),
('bj201106','唐伯虎2','男',22,'tb2h@163.com',11)
go
--插入方法二 只能执行前一句
insert into stuInfo(stuId,stuName,stuSex,stuAge,stuEamil,stuSeate)
select'bj201105','唐伯虎','男',11,'tbh@163.com',10 union
select'bj201106','唐伯虎2','男',22,'tb2h@163.com',11
go
--插入方法三 只能执行前一句
insert into stuInfo(stuId,stuName,stuSex,stuAge,stuEamil,stuSeate)
values('bj201105','唐伯虎','男',11,'tbh@163.com',10 )
insert into stuInfo(stuId,stuName,stuSex,stuAge,stuEamil,stuSeate)
values ('bj201106','唐伯虎2','男',22,'tb2h@163.com',11)
go
select * from stuMarks
select * from stuInfo
--delete触发器
if exists(select * from sysobjects where name='stuInfo_delete_TP')
drop trigger stuInfo_delete_TP
go
create trigger stuInfo_delete_TP
on TbL
for delete
as
print '开始删除数据'
if not exists(select * from sysobjects where name='temp_bk')
--从deleted表中查询所有的数据插入到新表中(temp_bk)
select * into temp_bk from deleted
else
--向已存在的表中插入多行数据
insert into temp_bk select * from deleted
select * from TBL
print '删除成功'
go
delete from TBL
select * from TBL
select * from temp_bk
--update触发器 系统中 先删除 在执行
if exists(select * from sysobjects where name='update_Account')
drop trigger update_Account
go
create trigger update_Account
on Account
for update
as
declare @CountID int
declare @Score money
select @CountID=CountID, @Score=Score from inserted
if not exists(select * from sysobjects where name='temp_log')
begin
create table temp_log
( logid int identity(1,1) primary key not null,
logtime datetime default(getdate()) not null,
CountID int not null,
Score money not null
)
print '@CountID'
insert into temp_log(CountID,Score)values(@CountID,@Score)
print '2'
end
else
insert into temp_log(CountID,Score)values(@CountID,@Score)
go
--drop trigger trigger_name 删除一个触发器
update Account set Score=35 where CountID=1
select * from Account
select * from temp_log
---instead of
if exists(select * from sysobjects where name='update_Instead')
drop trigger update_Instead
go
create trigger update_Instead
on stuMarks
instead of insert
as
if not exists(select * from stuInfo where stuId=(select stuID from inserted))
begin
rollback transaction
print '该学号不存在'
end
else
insert into stuMarks select * from inserted
print '已成功插入'
go
insert into stuMarks(testID,stuID,writeGrade,computerGrade) values ('E201101007','bj201101',100,100)
- 不同数据库请具体考虑