触发器入门
触发器的基础知识和例子
:create trigger tr_name
on table/view
{for | after | instead of } [update][,][insert][,][delete]
[with encryption]
as {batch | if update (col_name) [{and|or} update (col_name)] }
说明:
1 tr_name :触发器名称
2 on table/view :触发器所作用的表。一个触发器只能作用于一个表
3 for 和after :同义
4 after 与instead of :sql 2000新增项目afrer 与 instead of 的区别
after
在触发事件发生以后才被激活,只可以建立在表上
instead of
代替了相应的触发事件而被执行,既可以建立在表上也可以建立在视图上
5 insert、update、delete:激活触发器的三种操作,可以同时执行,也可选其一
6 if update (col_name):表明所作的操作对指定列是否有影响,有影响,则激活触发器。此外,因为delete 操作只对行有影响,
所以如果使用delete操作就不能用这条语句了(虽然使用也不出错,但是不能激活触发器,没意义)。
7 触发器执行时用到的两个特殊表:deleted ,inserted
deleted 和inserted 可以说是一种特殊的临时表,是在进行激活触发器时由系统自动生成的,其结构与触发器作用的表结构是一
样的,只是存放 的数据有差异。
续
下面说明deleted 与inserted 数据的差异
deleted 与inserted 数据的差异
inserted
存放进行insert和update 操作后的数据
deleted
存放进行delete 和update操作前的数据
注意:update 操作相当于先进行delete 再进行insert ,所以在进行update操作时,修改前的数据拷贝一条到deleted 表中,修改后
的数据在存到触发器作用的表的同时,也同时生成一条拷贝到insered表中
/////////
create trigger [trigger admixture_receive_log] on dbo.chl_lydj
for update
as
begin
declare @djsfxg char(10) declare @wtbh char(20)
select @wtbh=wtbh from inserted
update ly_tzk set djsfxg='已修改' where wtbh=@wtbh
end
if (select data_sfjl from t_logsetup)='是'
begin
declare @oldcjmc char (100) declare @oldlyrq datetime
declare @oldbzbh char (60) declare @oldzl char (20)
declare @olddj char (10)
declare @newcjmc char (100) declare @newlyrq datetime
declare @newbzbh char (60) declare @newzl char (20)
declare @newdj char (10)
declare @xgr char (20)
select @oldcjmc=cjmc,@oldlyrq=lyrq,@oldbzbh=bzbh,@oldzl=zl,@olddj=dj from deleted
select @newcjmc=cjmc,@newlyrq=lyrq,@newbzbh=bzbh,@newzl=zl,@newdj=dj from inserted
select @xgr=xgr from t_modifyuser where @wtbh=wtbh
if @oldcjmc<>@newcjmc
begin
insert into t_modifylog (wtbh, mod_time, mod_table, mod_field, ori_value, now_value, mod_people) values
(@wtbh,getdate(), 'chl_lydj','cjmc', @oldcjmc, @newcjmc, @xgr)
end
end
//////////修改时,直接把‘create’改为‘alter’即可
/////////////////////////
create trigger [trigger ly_tzk_syf] on dbo.ly_tzk
for insert
as
begin
declare @clmc char(100) declare @dwbh char(100) declare @syf char(100) declare @dwgcbh char(100) declare @wtbh char(50)
declare @dj_1 money declare @feiyong_z money declare @feiyong_xf money declare @feiyong_sy money
declare @dj char(20)
select @wtbh=wtbh , @clmc=clmc , @dwbh=dwbh ,@syf=syf from inserted
select @dj=dj from feihao_bz where clmc=@clmc
select @feiyong_z=feiyong_z, @feiyong_xf=feiyong_xf, @feiyong_sy=feiyong_sy from gongchengxinxi where dwgcbh=@dwbh
set @dj_1=convert(money ,@dj)
if @dj_1 <>0
begin
set @feiyong_xf=@feiyong_xf+@dj_1
set @feiyong_sy=@feiyong_sy-@dj_1
update ly_tzk set syf=@dj where wtbh=@wtbh
update gongchengxinxi set feiyong_xf=@feiyong_xf, feiyong_sy=@feiyong_sy where dwgcbh=@dwbh
end
else update ly_tzk set syf=convert(char , 0.0) where wtbh=@wtbh
end
//////////////////////
create trigger [trigger ly_tzk_syf_shanchu] on dbo.ly_tzk
for delete
as
begin
declare @clmc char(100) declare @dwbh char(100) declare @dwgcbh char(100) declare @wtbh char(50)
declare @feiyong_z money declare @feiyong_xf money declare @feiyong_sy money
declare @syf char(100) declare @syf_1 money
--declare @dj char(20) declare @dj_1 money
select @wtbh=wtbh , @clmc=clmc , @dwbh=dwbh ,@syf=syf from inserted
--select @dj=dj from feihao_bz where clmc=@clmc
select @feiyong_z=feiyong_z, @feiyong_xf=feiyong_xf, @feiyong_sy=feiyong_sy from gongchengxinxi where dwgcbh=@dwbh
set @syf_1=convert(money ,@syf)
if @syf_1 <>0
begin
set @feiyong_xf=@feiyong_xf-@syf_1
set @feiyong_sy=@feiyong_sy+@syf_1
update gongchengxinxi set feiyong_xf=@feiyong_xf, feiyong_sy=@feiyong_sy where dwgcbh=@dwbh
end
end
上一篇: 夫妻关系如何调整——避免成为半路夫妻
下一篇: 和男朋友吵架分手需要认错嘛,怎么和好