日期:2014-05-19  浏览次数:20527 次

使用触发器更新一列时,如何更新该表中另外的列?
create   table   A(code   int   identity(1,1),name   varchar(8)   not   null,address   varchar(100),lastmodified   datetime   not   null   default(getdate()));
insert   into   A(name,address,lastmodified)
select   '张三 ', '山东 ',getdate()
union   all   select   '李四 ', '山东 ',getdate()
union   all   select   '王五 ', '太原 ',getdate()
select   *   from   A;
go
if   exists(select   name   from   sysobjects   where   name= 'tr_1 '   and   type= 'tr ')   drop   trigger   tr_1;
go
create   trigger   tr_1
on   A
for   update
as
if   update(address)
update   A   set   lastmodified=getdate()
go
update   A   set   address= '山西 '   where   code=2;
select   *   from   A
drop   table   A
go


------解决方案--------------------
--try

create trigger tr_1
on A
for update
as
if update(address)
update A set lastmodified=getdate()
from inserted
where A.code=inserted.code
go