SQL 监控表字段变化
SQL新建触发器监控某个表上的字段新增,更新,删除时记录在一个新建的数据库日志表上
以下示例以,在表table_monitor上新增触发器tr_sto,监控表上的field_monitor的变化值,并将相关信息记录在新建表log_sto上
create table log_sto (logid int not null identity(1,1), -- 日志序号(日志主键) operate varchar(10), -- 操作类型 如Insert,Update,Delete. id int, -- 原表ID(主键) old_de nvarchar(255), -- de字段旧值 new_de nvarchar(255), -- de字段新值 spid int not null, -- spid login_name varchar(100), -- 登录名 prog_name varchar(100), -- 程序名 hostname varchar(100), -- 主机名 ipaddress varchar(100), -- IP地址 runsql varchar(8000), -- 执行的TSQL代码 UDate datetime -- 操作日期时间 constraint pk_logsto primary key(logid) ) -- 建跟踪触发器 create trigger tr_sto on table_monitor after update,insert,delete --需要监控的表 as begin declare @di table(et varchar(200),pt varchar(200),ei varchar(max)) insert into @di exec('dbcc inputbuffer(@@spid) WITH NO_INFOMSGS') --显示从客户端发送到 Microsoft® SQL Server™ 的最后一个语句。 declare @op varchar(10) select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted) then 'Update' when exists(select 1 from inserted) and not exists(select 1 from deleted) then 'Insert' when not exists(select 1 from inserted) and exists(select 1 from deleted) then 'Delete' end if @op in('Update','Insert') begin insert into log_sto (operate,id,old_de,new_de,spid,login_name,prog_name,hostname,ipaddress,runsql,UDate) select @op,n.id,o.field_monitor,n.field_monitor,@@spid, --需要监控的表的字段 (select login_name from sys.dm_exec_sessions where session_id=@@spid), (select program_name from sys.dm_exec_sessions where session_id=@@spid), (select hostname from sys.sysprocesses where spid=@@spid), (select client_net_address from sys.dm_exec_connections where session_id=@@spid), (select top 1 isnull(ei,'') from @di), getdate() from inserted n left join deleted o on o.id=n.id --需要监控的表的主键字段,主要作用在于记录是哪个记录发生了变化 end else begin insert into log_sto (operate,id,old_de,new_de,spid,login_name,prog_name,hostname,ipaddress,runsql,UDate) select @op,o.FClassTypeID,o.FBusinessLevel,null,@@spid, (select login_name from sys.dm_exec_sessions where session_id=@@spid), (select program_name from sys.dm_exec_sessions where session_id=@@spid), (select hostname from sys.sysprocesses where spid=@@spid), (select client_net_address from sys.dm_exec_connections where session_id=@@spid), (select top 1 isnull(ei,'') from @di), getdate() from deleted o end end go