有两个表,表A: 编号 名称
表B: 编号 名称
当然表A,B中不止这个两个字段,但是和其他字段没关系
怎么样当在表B中输入的编号和表A一样是时候,触发出表B的名称=表A的名称。问题补充:
以上的编号的主键唯一的,但是如果编号不是唯一的也就是说一个编号,有可能对两个以上的名称,那样可以写触发器吗?
表B: 编号 名称
当然表A,B中不止这个两个字段,但是和其他字段没关系
怎么样当在表B中输入的编号和表A一样是时候,触发出表B的名称=表A的名称。问题补充:
以上的编号的主键唯一的,但是如果编号不是唯一的也就是说一个编号,有可能对两个以上的名称,那样可以写触发器吗?
on B
after update
as
begin
update a set name=b.name
from a
inner join deleted b on a.id=b.idend
on B
for insert,update
as
if exists(select 1 from inserted t where exists(select 1 from A where t.编号=编号))
update B set 名称=a.名称 from inserted a where a.编号=b.编号
go
create trigger up_b on B after update
as
declare @名稱 varchar
begin
Select @名稱=a.名稱 from a, inserted ins where a.編號=ins.編號
update b Set 名稱=@名稱 from b, inserted ins where b.編號=ins.編號
end
on B
for insert,update
as
--if exists(select 1 from inserted t where exists(select 1 from A where t.编号=编号))
update B set 名称=a.名称 from inserted a where a.编号=b.编号 --有加where可以省去IF判断
go
on B
for insert,update
as
if
if exists(select 1 from inserted t where exists(select 1 from A where t.编号=编号))
update B set 名称=a.名称 from inserted a where a.编号=b.编号
go