觸發器的基礎知識和例子 :create trigger tr_name on table/view {for | after | instead of } [update][,][insert][,][delete] [with encryption] as {batch | if update ( ...
觸發器的基礎知識和例子
: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