请教一个insert的时候的触发器
现在是某个操作之后会向A表插入一条数据,A表有个字段叫诊断,之前这个诊断都是插入的是诊断编码,现在想当向A表insert的时候自动关联update成B表的诊断名称,A跟B都有共同字段诊断编码
请大神帮忙写个触发器!
------解决方案--------------------create trigger trg_adddata on A
after insert
as
begin
update A
set A.诊断 = B.诊断名称
from A inner join B where A.诊断编码= B.诊断编码
where not exists(select 1 from B where A.诊断 = B.诊断名称)
end
--参考,这个实际上在前台做处理更好。
------解决方案--------------------create trigger .... on a
after insert
as
update A
set a.诊断=b.诊断名 from b where exists(select 1 from inserted i where a.编码=i.编码)
------解决方案--------------------update A set A.诊断 = B.诊断名称 from B where A.诊断 = B.诊断 AND A.诊断 IN (SELECT 诊断 FROM inserted)
------解决方案--------------------create trigger a_ins on A
for insert
as
begin
declare @bh nvarchar(50)
select @bh=诊断编码 from inserted
update A set A.诊断 = B.诊断名称
from A inner join B on A.诊断编码= B.诊断编码 and B.诊断编码=@bh
end