日期:2014-05-17  浏览次数:20629 次

SQL Server2008 中,写一个触发器,根据一个表中新插入的记录修改另一个表
SQL Server2008 中,要求写一个触发器在表A每次添加新记录时触发,
若新记录中aa字段的值在表B中ba字段不存在,则修改表B中的另一个字段bb值为表A中的aa的值。 

create trigger tri_test on A after insert
    as if exists(select aa from (select ba from B))
begin    
    update B inner join A where A.id=B.id set B.bb=A.aa;
end 

请问哪里写的不对,应该怎么写? 

菜鸟一枚,第一次发帖,分比较少,大神们见谅!
SQL?Server 触发器 update 两表操作

------解决方案--------------------
 as if exists(select aa from (select ba from B))
 这句就不对..
 as if exists(select aa from (select ba from B)a) 这么写试试 

------解决方案--------------------
create trigger tri_test on A after insert
    as 
begin
declare @count int
select @count=count(*) from inserted t inner join B on t.aa=B.ba

if @count=0
begin    
update B set B.bb=A.aa
from inserted --插入表
where inserted.id=B.id ;
end 
end

------解决方案--------------------
lz 你看一下触发器吧
对于insert触发器,对应的inserted 表