在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、什么是触发器 触发器对表进行插入、更新、删除的时候会自动执行的特殊存储过程。触发器一般用在check约束更加复杂的约束上面。触发器和普通的存储过程的区别是:触发器是当对某一个表进行操作。诸如: 2、 DML触发器分为2.1 after触发器(之后触发)a、 insert触发器 b、 update触发器 c、 delete触发器 2.2 instead of 触发器 (之前触发)其中after触发器要求只有执行某一操作insert、update、delete之后触发器才被触发,且只能定义在表上。而 触发器有两个特殊的表:插入表(
Update数据的时候就是先删除表记录,然后增加一条记录。这样在 3、 创建触发器语法 create trigger tgr_name on table_name with encrypion –加密触发器 for update... as Transact-SQL 3.1 创建insert类型触发器--创建insert插入类型触发器 if (object_id('tgr_classes_insert', 'tr') is not null) drop trigger tgr_classes_insert go create trigger tgr_classes_insert on classes for insert --插入触发 as --定义变量 declare @id int, @name varchar(20), @temp int; --在inserted表中查询已经插入记录信息 select @id = id, @name = name from inserted; set @name = @name + convert(varchar, @id); set @temp = @id / 2; insert into student values(@name, 18 + @id, @temp, @id); print '添加学生成功!'; go --插入数据 insert into classes values('5班', getDate()); --查询数据 select * from classes; select * from student order by id;
3.2 创建delete类型触发器--delete删除类型触发器 if (object_id('tgr_classes_delete', 'TR') is not null) drop trigger tgr_classes_delete go create trigger tgr_classes_delete on classes for delete --删除触发 as print '备份数据中……'; if (object_id('classesBackup', 'U') is not null) --存在classesBackup,直接插入数据 insert into classesBackup select name, createDate from deleted; else --不存在classesBackup创建再插入 select * into classesBackup from deleted; print '备份数据成功!'; go -- --不显示影响行数 --set nocount on; delete classes where name = '5班'; --查询数据 select * from classes; select * from classesBackup; 3.3 创建update类型触发器--update更新类型触发器 if (object_id('tgr_classes_update', 'TR') is not null) drop trigger tgr_classes_update go create trigger tgr_classes_update on classes for update as declare @oldName varchar(20), @newName varchar(20); --更新前的数据 select @oldName = name from deleted; if (exists (select * from student where name like '%'+ @oldName + '%')) begin --更新后的数据 select @newName = name from inserted; update student set name = replace(name, @oldName, @newName) where name like '%'+ @oldName + '%'; print '级联修改数据成功!'; end else print '无需修改student表!'; go --查询数据 select * from student order by id; select * from classes; update classes set name = '五班' where name = '5班';
3.4 update更新列级触发器if (object_id('tgr_classes_update_column', 'TR') is not null) drop trigger tgr_classes_update_column go create trigger tgr_classes_update_column on classes for update as --列级触发器:是否更新了班级创建时间 if (update(createDate)) begin raisError('系统提示:班级创建时间不能修改!', 16, 11); rollback tran; end go --测试 select * from student order by id; select * from classes; update classes set createDate = getDate() where id = 3; update classes set name = '四班' where id = 7; 更新列级触发器可以用 4、instead of类型触发器
创建语法: create trigger tgr_name on table_name with encryption instead of update... as T-SQL 4.1 创建instead of触发器if (object_id('tgr_classes_inteadOf', 'TR') is not null) drop trigger tgr_classes_inteadOf go create trigger tgr_classes_inteadOf on classes 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; --先删除student的信息 delete student where cid = @id; --再删除classes的信息 delete classes where id = @id; print '删除[ id: ' + convert(varchar, @id) + ', name: ' + @name + ' ] 的信息成功!'; go --test select * from student order by id; select * from classes; delete classes where id = 7; 4.2 显示自定义消息raiserrorif (object_id('tgr_message', 'TR') is not null) drop trigger tgr_message go create trigger tgr_message on student after insert, update as raisError('tgr_message触发器被触发', 16, 10); go --test insert into student values('lily', 22, 1, 7); update student set sex = 0 where name = 'lucy'; select * from student order by id; 4.3 修改触发器alter trigger tgr_message on student after delete as raisError('tgr_message触发器被触发', 16, 10); go --test delete from student where name = 'lucy'; 4.4 启用、禁用触发器--禁用触发器 disable trigger tgr_message on student; --启用触发器 enable trigger tgr_message on student; 4.5 查询创建的触发器信息--查询已存在的触发器 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 'tgr_message'; 4.6 示例,验证插入数据if ((object_id('tgr_valid_data', 'TR') is not null)) drop trigger tgr_valid_data go create trigger tgr_valid_data on student after insert as declare @age int, @name varchar(20); select @name = s.name, @age = s.age from inserted s; if (@age < 18) begin raisError('插入新数据的age有问题', 16, 1); rollback tran; end go --test insert into student values('forest', 2, 0, 7); insert into student values('forest', 22, 0, 7); select * from student order by id; 4.7 示例,操作日志if (object_id('log', 'U') is not null) drop table log go create table log( id int identity(1, 1) primary key, action varchar(20), createDate datetime default getDate() ) go if (exists (select * from sys.objects where name = 'tgr_student_log')) drop trigger tgr_student_log go create trigger tgr_student_log on student after insert, update, delete as if ((exists (select 1 from inserted)) and (exists (select 1 from deleted))) begin insert into log(action) values('updated'); end else if (exists (select 1 from inserted) and not exists (select 1 from deleted)) begin insert into log(action) values('inserted'); end else if (not exists (select 1 from inserted) and exists (select 1 from deleted)) begin insert into log(action) values('deleted'); end go --test insert into student values('king', 22, 1, 7); update student set sex = 0 where name = 'king'; delete student where name = 'king'; select * from log; select * from student order by id; 到此这篇关于 |
请发表评论