update触发器问题
表A
ID(主键) name col3...
1 aaa .
2 bbb .
3 ccc .
表B
ID(外键) name col3...
1 aaa .
2 bbb .
3 ccc .
表A,B设置了级联更新和删除,求update触发器使在更新表A的ID,name字段时能同时
更新表B的ID和name字段.即如在把A中的2 bbb修改为4 ddd时,B中的2 bbb也被修改
成4 ddd.
------解决方案--------------------create trigger t_tri
on A
for update
as
if update(ID)
update a set ID=(select ID from inserted where ID=a.ID)
from B a inner Join deleted c on a.ID=c.ID
if update(name)
update a set name=c.name from B a inner Join inserted c on a.ID=c.ID
------解决方案----------------------可以这样做
create trigger t_tri
on A
for update
as
if update(ID) or update(Name)
update B set ID=inserted.ID,Name=inserted.Name from B,inserted,deleted
where B.ID=deleted.ID and B.Name=deleted.Name