create tr_update on A for insert asif update([name])
update A set re = re + '字段名为name,原为:' + B.name + ' 修改为:' + A.name + ';' from A, deleted B where A.id = B.id
if update(sex)
update A set re = re + '字段名为sex,原为:' + B.sex + ' 修改为:' + A.sex + ';' from A, deleted B where A.id = B.id
if update(country)
update A set re = re + '字段名为country,原为:' + B.country + ' 修改为:' + A.country + ';' from A, deleted B where A.id = B.id
if update(city)
update A set re = re + '字段名为city,原为:' + B.city + ' 修改为:' + A.city + ';' from A, deleted B where A.id = B.id
update A set re = re + '字段名为name,原为:' + B.name + ' 修改为:' + A.name + ';' from A, deleted B where A.id = B.id
if update(sex)
update A set re = re + '字段名为sex,原为:' + B.sex + ' 修改为:' + A.sex + ';' from A, deleted B where A.id = B.id
if update(country)
update A set re = re + '字段名为country,原为:' + B.country + ' 修改为:' + A.country + ';' from A, deleted B where A.id = B.id
if update(city)
update A set re = re + '字段名为city,原为:' + B.city + ' 修改为:' + A.city + ';' from A, deleted B where A.id = B.id
CREATE TRIGGER TR_A_UPDATE
ON A
FOR UPDATE
AS
IF NOT UPDATE(name) AND NOT UPDATE(sex) AND NOT UPDATE(country) AND NOT UPDATE(city)
RETURNUPDATE A
SET REMART=CASE WHEN A.REMARK LIKE '%'+CONVERT(CHAR(10),GETDATE(),120)+'%' THEN A.REMART+';'+CONVERT(CHAR(10),GETDATE(),120)+',name:'+D.NAME+'->'+I.NAME
ELSE A.REMART+',name:'+D.NAME+'->'+I.NAME
END
FROM A,INSERTED I,DELETED D
WHERE A.ID=I.ID
AND I.ID=D.ID
AND I.NAME<>D.NAMEUPDATE A
SET REMART=CASE WHEN A.REMARK LIKE '%'+CONVERT(CHAR(10),GETDATE(),120)+'%' THEN A.REMART+';'+CONVERT(CHAR(10),GETDATE(),120)+',sex:'+D.sex+'->'+I.sex
ELSE A.REMART+',sex:'+D.sex+'->'+I.sex
END
FROM A,INSERTED I,DELETED D
WHERE A.ID=I.ID
AND I.ID=D.ID
AND I.sex<>D.sexUPDATE A
SET REMART=CASE WHEN A.REMARK LIKE '%'+CONVERT(CHAR(10),GETDATE(),120)+'%' THEN A.REMART+';'+CONVERT(CHAR(10),GETDATE(),120)+',country:'+D.country+'->'+I.country
ELSE A.REMART+',country:'+D.country+'->'+I.country
END
FROM A,INSERTED I,DELETED D
WHERE A.ID=I.ID
AND I.ID=D.ID
AND I.country<>D.countryUPDATE A
SET REMART=CASE WHEN A.REMARK LIKE '%'+CONVERT(CHAR(10),GETDATE(),120)+'%' THEN A.REMART+';'+CONVERT(CHAR(10),GETDATE(),120)+',city:'+D.city+'->'+I.city
ELSE A.REMART+',city:'+D.city+'->'+I.city
END
FROM A,INSERTED I,DELETED D
WHERE A.ID=I.ID
AND I.ID=D.ID
AND I.city<>D.cityGO
create table A(id int,[name] varchar(20),sex char(1),country varchar(30),city varchar(30),re varchar(4000))
insert into A select 1,'aaa','w','china','beijing',null
insert into A select 2,'bbb','m','chinass','dalian',null
go
create trigger tri_test on A
for update
as
begin
declare @str varchar(4000)
set @str=''
if update([name])
select @str=@str+convert(varchar(20),getdate(),120)+'修改了如下信息:字段名:name='+d.[name]+';修改为'+i.[name] from deleted d,inserted i where i.id=d.id
if update(sex)
select @str=@str+convert(varchar(20),getdate(),120)+'修改了如下信息:字段名:'+d.sex+';修改为'+i.[sex] from deleted d,inserted i where i.id=d.id
if update(country)
select @str=@str+convert(varchar(20),getdate(),120)+'修改了如下信息:字段名:'+d.country+';修改为'+i.[country] from deleted d,inserted i where i.id=d.id
if update(city)
select @str=@str+convert(varchar(20),getdate(),120)+'修改了如下信息:字段名:'+d.city+';修改为'+i.[city] from deleted d,inserted i where i.id=d.id if @str<>''
update a set a.re=isnull(a.re,'')+' '+@str from A a,deleted b where a.id=b.idend
go
--测试
update a set sex='r' where id=2
select * from a
--结果
id name sex country city re
1 aaa w china beijing NULL
2 bbb r chinass dalian 2004-09-07 15:01:34修改了如下信息:字段名:m;修改为r
只修改REMARK,直接退出就可以了!感觉这个功能如果是三层的话,在中间层处理比较好!