首页 > 代码库 > SQL SERVER 触发器
SQL SERVER 触发器
触发器涉及两个特殊的表:inserted、deleted
~~~~~~~~~~~~~~~
inserted表说明 ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1、创建一个insert触发器(插入后出发)
create trigger trg_person_insert on person for insert as select * from inserted go
2、然后插入一条记录
insert into person values(‘kk‘, 1, ‘1993-05-23‘, ‘A‘, ‘A‘, ‘A‘)
3、输出结果
name age birth study sport art
kk 1 1993-05-23 A A A
4、所以insert操作在inserted表插入记录
--------------------------------------------------------------------
1、创建一个update触发器(更新后出发)
create trigger trg_person_update on person for update as select * from inserted go
2、然后更新刚才插入的记录
update person set name = ‘TT‘ where name = ‘kk‘
3、输出结果
name age birth study sport art
TT 1 1993-05-23 A A A
4、所以update操作在inserted表插入更新后的记录
--------------------------------------------------------------------
1、创建一个update触发器(更新后出发)
create trigger trg_person_update on person for update as select * from inserted go
2、然后更新刚才插入的记录
update person set name = ‘TT‘ where name = ‘kk‘
3、输出结果
name age birth study sport art
TT 1 1993-05-23 A A A
4、所以update操作在inserted表插入更新后的记录
-----------------------------------------------------------------------
1、创建一个delete触发器(删除后出发)
create trigger trg_person_delete on person for delete as select * from inserted go
2、然后删除刚才插入的记录
delete from person where name = ‘TT‘
3、输出结果
name age birth study sport art
4、所以delete操作并不影响inserted表
********************************************************************************
~~~~~~~~~~~~~~~~
inserted表说明 ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1、创建一个insert触发器(插入后出发)
create trigger trg_person_insert on person for insert as select * from deleted go
2、然后插入一条记录
insert into person values(‘cc‘, 1, ‘1993-05-23‘, ‘A‘, ‘A‘, ‘A‘)
3、输出结果
name age birth study sport art
4、所以insert操作不影响deleted表的数据
----------------------------------------------------------------------------
1、创建一个update触发器(更新后出发)
create trigger trg_person_update on person for update as select * from deleted go
2、然后更新刚才插入的记录
update person set name = ‘kk‘ where name = ‘cc‘
3、输出结果
name age birth study sport art
kk 1 1993-05-23 A A A
4、所以update操作在deleted表插入更新后的记录
----------------------------------------------------------------------------
1、创建一个delete触发器(删除后出发)
create trigger trg_person_delete on person for delete as select * from deleted go
2、然后删除刚才插入的记录
delete from person where name = ‘kk‘
3、输出结果
name age birth study sport art
kk 1 1993-05-23 A A A
4、所以delete操作在deleted表插入删除的记录
******************************************************************************8*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
after触发器和instead of触发器比较 ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
instead of触发器是在操作执行之前触发
1、创建一个instead of insert触发器
create trigger trg_person_insert_insteadof on person instead of insert as select * from person go
2、插入一条记录
insert into person values(‘kk‘, 1, ‘1993-05-23‘, ‘A‘, ‘A‘, ‘A‘)
3、输出结果
name age birth study sport art
1 1993-05-23 A A A
Hasaki 3 1983-08-29 A A A
Noki 23 1993-05-02 A B C
Yummy 20 1996-09-03 S B B
4、所以instead of触发器是在操作执行前触发
-----------------------------------------------------------------------------------
after触发器是在操作执行之后触发
1、创建一个instead of insert触发器
create trigger trg_person_insert on person for insert as select * from person go
2、插入一条记录
insert into person values(‘cc‘, 1, ‘1993-05-23‘, ‘A‘, ‘A‘, ‘A‘)
3、输出结果
name age birth study sport art
1 1993-05-23 A A A
cc 1 1993-05-23 A A A
Hasaki 3 1983-08-29 A A A
Noki 23 1993-05-02 A B C
Yummy 20 1996-09-03 S B B
4、所以after触发器是在操作执行后触发
************************************************************************************
~~~~~~~~~~~~~~~
列级更新触发器~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1、创建一个update触发器,在name字段被更新时触发
create trigger trg_person_update on person instead of update as if(update(name)) begin select * from inserted select * from deleted end go
2、更新name字段
update person set name = ‘noki‘ where name = ‘Noki‘
3、输出结果
name age birth study sport art
noki 23 1993-05-02 A B C
name age birth study sport art
Noki 23 1993-05-02 A B C
4、更新age字段
update person set age = 3 where name = ‘noki‘
5、输出结果
(1 行受影响)
6、所以,触发器确实只有在更新name字段时才触发
同时发现,update操作在inserted表插入更新后的记录,在deleted表插入更新前的记录
**************************************************************************************
小结:
----------------------------------------------------
| 操作 | inserted表 | deleted 表 |
----------------------------------------------------
| insert | 存放插入的记录 | 不影响 |
----------------------------------------------------
| update | 存放更新后记录 | 存放更新前记录 |
----------------------------------------------------
| delete | 不影响 | 存放删除的记录 |
----------------------------------------------------
-----------------------------------------
| after触发器 | instead of触发器 |
-----------------------------------------
| 操作执行后触发 | 操作执行前触发 |
-----------------------------------------
SQL SERVER 触发器