UPDATE触发器问题!
我有两个表
表一:user
uid|password|name|department|duty|
表二:user_detail
uid|name|sex|department|duty|phone|fax|mobile|等
要求 在user中更新uid或者name|或者department或duty时,同时user_detail中的对应字段也相应更新。INSERT和DELETE触发器已经完成。
请问UPDATE触发器怎么写?
已知IF (CCOLUMNS_UPDATED()&29)>0 可以判断1,3,4,5列是否更新
问题点数:100、回复次数:1Top
1 楼zjcxc(邹建)回复于 2005-04-01 19:27:08 得分 100
--如果uid是主键,如果同时更新了多条记录的uid的话,可能导致inserted和deleted表的记录无法对应
--所以如果要写update触发器的话,在uid被更新时,要求限制只能更新一条记录
--触发器如下
create trigger tr_user_update on [user]
for update
as
declare @row int
set @row=@@rowcount
if @row=0 return
if update(uid) and @row>1
begin
raiserror('更新了uid时,不允许更新多条记录,本次操作取消',1,16)
rollback tran
return
end
if (COLUMNS_UPDATED()&29)>0
if @row=1
update b set
uid=i.uid,name=i.name,department=i.department,duty=i.duty
from user_detail b,inserted i,deleted d
where b.uid=d.uid
else
update b set
uid=i.uid,name=i.name,department=i.department,duty=i.duty
from user_detail b,inserted i
where b.uid=i.uid
Top




