触发器:两个表有三个字段相同,其中一个插入数据时,需要更新另外一个对应的三个字段
a
ren1 tel1 ren2
b
ren1 tel1 ren2
a表是业务表,经常有数据插入,b表是用户信息表,只有在a插入的记录里此三字段不全为空的时候才更新b表
a、b共同的字段是danwei
------解决方案--------------------create trigger tg_test
on a
for insert
as
begin
declare @a varchar(20), @b varchar(20), @c varchar(20), @d varchar(20)
select @a = ren1, @b = tel1, @c = ren2, @d = danwei
from inserted
if ((@a is not null) or (@b is not null) or (@c is not null))
begin
update b
set ren1 = @a, tel1 = @b, ren2 = @c
where danwei = @d
end
end