在触发器内部判断是添加、修改、还是删除:
alter trigger tr_skywfAgentConfig
on skywfAgentConfig
after insert,update,delete
as begin
declare @add bit,@del bit,@act varchar(10)
select @add=1 where exists(select 1 from inserted)
select @del=1 where exists(select 1 from deleted)
if @add=1 and @del=1 set @act='Modify'
if @add=1 and @del is null set @act='Add'
if @add is null and @del=1 set @act='Delete'
if @add=1
insert skywfAgentConfig_log
select ID,EID,Agent,TypeID,KeyValue,Flows,StartDate,EndDate,xOrder,xRemark
,getdate(),@act from inserted
else
insert skywfAgentConfig_log
select ID,EID,Agent,TypeID,KeyValue,Flows,StartDate,EndDate,xOrder,xRemark
,getdate(),@act from deleted
end
类似的,我们还可以通过触发器来生成数据变动的sql语句,然后把这些语句放到其他数据库执行,轻松实现数据同步功能。