触发器是一种特殊类型的存储过程, 它不同于之前的我们介绍的存储过程.
触发器主要是通过事件进行触发被自动调用执行的. 而存储过程可以通过存储过程的名称被调用.
SQL Server 2005 中触发器可以分为两类: DML 触发器和 DDL 触发器, 其中 DDL 触发器它们会影响
多种数据定义语言语句而激发, 这些语句有 create,alter,drop 语句.
(在此仅讨论 DML 触发器)
DML 触发器分为:
1, after 触发器(之后触发)
a, insert 触发器
b, update 触发器
c, delete 触发器
2, instead of 触发器 (之前触发)
其中 after 触发器要求只有执行某一操作 insert,update,delete 之后触发器才被触发, 且只能定义在表上.
而 instead of 触发器表示并不执行其定义的操作 (insert,update,delete) 而仅是执行触发器本身.
既可以在表上定义 instead of 触发器, 也可以在视图上定义.
触发器有两个特殊的表: 插入表 (instered 表) 和删除表(deleted 表). 这两张是逻辑表也是虚表.
有系统在内存中创建者两张表, 不会存储在数据库中. 而且两张表的都是只读的, 只能读取数据而不能修改数据.
这两张表的结果总是与被改触发器应用的表的结构相同. 当触发器完成工作后, 这两张表就会被删除.
Inserted 表的数据是插入或是修改后的数据, 而 deleted 表的数据是更新前的或是删除的数据.
对表的操作 | Inserted 逻辑表 | Deleted 逻辑表 |
增加记录(insert) | 存放增加的记录 | 无 |
删除记录(delete) | 无 | 存放被删除的记录 |
修改记录(update) | 存放更新后的记录 | 存放更新前的记录 |
Update 数据的时候就是先删除表记录, 然后增加一条记录. 这样在 inserted 和 deleted 表就都有 update 后的数据记录了.
注意: 触发器本身就是一个事务, 所以在触发器里面可以对修改数据进行一些特殊的检查. 如果不满足可以利用事务回滚, 撤销操作.
--delete 删除类型触发器
if (object_id('Trg_班级表_delete', 'TR') is not null)
drop trigger Trg_班级表_delete
go
create trigger Trg_班级表_delete
on 班级表
for delete -- 删除触发
- as
- print '备份数据中......';
- if (object_id('班级备份表', 'U') is not null)
-- 存在 classesBackup, 直接插入数据
insert into 班级备份表
- select name, createDate from deleted;
- else
-- 不存在班级备份表, 创建再插入
select * into 班级备份表 from deleted;
- print '备份数据成功!';
- go
- --
-- 不显示影响行数
--set nocount on;
--delete 触发器会在删除数据的时候, 将刚才删除的数据保存在 deleted 表中
delete 班级表 where name = '5 班';
-- 查询数据
select * from 班级表;
select * from 班级备份表;
--update 更新类型触发器
if (object_id('Trg_班级表_update', 'TR') is not null)
drop trigger Trg_班级表_update
go
create trigger Trg_班级表_update
on 班级表
- for update
- as
- declare @oldName varchar(20), @newName varchar(20);
-- 更新前的数据
- select @oldName = name from deleted;
- if (exists (select * from 学生表 where name like '%'+ @oldName + '%'))
- begin
-- 更新后的数据
select @newName = name from inserted;
update 学生表 set name = replace(name, @oldName, @newName) where name like '%' @oldName + '%';
- print '级联修改数据成功!';
- end
- else
- print '无需修改学生表!';
- go
-- 查询数据
--update 触发器会在更新数据后, 将更新前的数据保存在 deleted 表中, 更新后的数据保存在 inserted 表中
select * from 学生表 order by id;
select * from 班级表;
update 班级表 set name = '五班' where name = '5 班';
--update 更新列级触发器
if (object_id('Trg_班级表_update_column', 'TR') is not null)
drop trigger Trg_班级表_update_column
go
create trigger Trg_班级表_update_column
on 班级表
for update
as
-- 列级触发器: 是否更新了班级的字段[创建时间]
- if (update(createDate))
- begin
- raisError('系统提示: 班级创建时间不能修改!', 16, 11);
- rollback tran;
- end
- go
-- 测试
-- 更新列级触发器可以用 update 是否判断更新列记录;
select * from 学生表 order by id;
select * from 班级表;
update 班级表 set createDate = getDate() where id = 3;
update 班级表 set name = '四班' where id = 7;
-- 创建 instead of 触发器
if (object_id('Trg_班级表_inteadOf', 'TR') is not null)
drop trigger Trg_班级表_inteadOf
go
create trigger Trg_班级表_inteadOf
on 班级表
- instead of delete/*, update, insert*/
- as
- declare @id int, @name varchar(20);
-- 查询被删除的信息, 病赋值
- select @id = id, @name = name from deleted;
- print 'id:' + convert(varchar, @id) + ', name:' + @name;
-- 先删除学生表的信息
delete 学生表 where cid = @id;
-- 再删除班级表的信息
delete 班级表 where id = @id;
- print '删除[ id:' + convert(varchar, @id) + ', name:' + @name + '] 的信息成功!';
- go
- --test
select * from 学生表 order by id;
select * from 班级表;
delete 班级表 where id = 7;
-- 创建触发器语法
- create trigger Trg_name
- on table_name
with encrypion - 加密触发器
- for update...
- as
- Transact-SQL
-- 显示自定义消息 raiserror
- if (object_id('Trg_message', 'TR') is not null)
- drop trigger Trg_message
- go
- create trigger Trg_message
on 学生表
- after insert, update
- as raisError('Trg_message 触发器被触发', 16, 10);
- go
- --test
insert into 学生表 values('lily', 22, 1, 7);
update 学生表 set sex = 0 where name = 'lucy';
select * from 学生表 order by id;
-- 禁用触发器
disable trigger Trg_message on 学生表;
-- 启用触发器
enable trigger Trg_message on 学生表;
-- 查询已存在的触发器
- select * from sys.triggers;
- select * from sys.objects where type = 'TR';
-- 查看触发器触发事件
- select te.* from sys.trigger_events te join sys.triggers t
- on t.object_id = te.object_id
- where t.parent_class = 0 and t.name = 'tgr_valid_data';
-- 查看创建触发器语句
exec sp_helptext 'Trg_message';
来源: http://www.bubuko.com/infodetail-3294363.html