触发器在制定的表中发生变化时被调用以响应insert,update,delete事件。
trigger可以查询其他表,包含复杂的T-SQL语句,并支持回滚.
每个触发器都会创建两个逻辑专用表:inserted 和 deleted表,表结构与被触发作用的表结构相同 执行完毕后 与触发器相关的两个表都会被删除。
当执行insert语句时,inserted表存放要向表中插入的所有行;当执行delete语句时,deleted表存放要向表中删除的所有行
当执行update语句时,相当于执行一个delete操作,再执行一个insert操作,旧的行先被移动到deleted表,然后再在新值插入到inserted表和目标表。
当执行update语句时,相当于执行一个delete操作,再执行一个insert操作,旧的行先被移动到deleted表,然后再在新值插入到inserted表和目标表。
例1
Table Name: TB_1,TB_2 (两个table 结构完全一样)
Create Table TB_1(ID Int, Name_1 varchar(20),Status varchar(20) ,Type Varchar(20))
Create Table TB_2(ID Int, Name_1 varchar(20),Status varchar(20) ,Type Varchar(20))
-- 为Tb_1创建Trigger, 当 TB_1 发生变化时,变化的信息(Insert ,Update,Delete)将保存于 TB_2 中,Update是两条记录,Update 之前和之后 都要保存盐类
Create trigger Trig_t2
On Tb_1
For Insert,Update,Delete
As
declare @id int,
@name_1 varchar(20),
@status varchar(20)
On Tb_1
For Insert,Update,Delete
As
declare @id int,
@name_1 varchar(20),
@status varchar(20)
If not exists(Select 1 from deleted) ----Insert
Begin
select @id=id, @name_1=name_1,@status=status from inserted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'Insert')
End
Begin
select @id=id, @name_1=name_1,@status=status from inserted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'Insert')
End
If exists(select 1 from inserted) and exists(select 1 from deleted) ---Update
Begin
/* Update Before */
select @id=id, @name_1=name_1,@status=status from deleted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'UpdateBefor')
Begin
/* Update Before */
select @id=id, @name_1=name_1,@status=status from deleted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'UpdateBefor')
/* Update End */
select @id=id, @name_1=name_1,@status=status from inserted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'UpdateEnd')
End
select @id=id, @name_1=name_1,@status=status from inserted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'UpdateEnd')
End
If not exists(select 1 from inserted) ---Delete
Begin
select @id=id, @name_1=name_1,@status=status from inserted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'Delete')
End
Begin
select @id=id, @name_1=name_1,@status=status from inserted
insert into tb_2(id,name_1,status,type)values(@id,@name_1,@status,'Delete')
End
----测试----
insert into tb_1(id,name_1,status)values('1','1','1')
select * from tb_1
select * from tb_2
select * from tb_1
select * from tb_2
id name_1 status type
----------- -------------------- -------------------- --------------------------------------------------
1 1 1 NULL
----------- -------------------- -------------------- --------------------------------------------------
1 1 1 NULL
(1 row(s) affected)
id name_1 status type
----------- -------------------- -------------------- --------------------------------------------------
1 1 1 Insert
----------- -------------------- -------------------- --------------------------------------------------
1 1 1 Insert
-----------测试Update---------------
update tb_1 set name_1='2'where id='1'
select * from tb_1
select * from tb_2
select * from tb_2
id name_1 status type
----------- -------------------- -------------------- --------------------------------------------------
1 2 1 NULL
----------- -------------------- -------------------- --------------------------------------------------
1 2 1 NULL
(1 row(s) affected)
id name_1 status type
----------- -------------------- -------------------- --------------------------------------------------
1 1 1 Insert
1 1 1 UpdateBefor
1 2 1 UpdateEnd
----------- -------------------- -------------------- --------------------------------------------------
1 1 1 Insert
1 1 1 UpdateBefor
1 2 1 UpdateEnd
例2 (instead of语句)
利用触发器instead of 语句代替其他语句操作 (就不执行instead of 后面指定的SQL指令而执行Trigger体 [SQL-statements])
use Db_Name go create trigger Trigger_Name on Table_name instead of [insert,update,delete] as [SQL_statements] go
例如在系统查询分析器执行 update t1 set column_1='aaa' where id=123 可以看到t1下123号的column_1值没有预期改变 而是按照Trigger中 [SQL-statements]中的格式走了