我要把A表的数据导入到B表,两个表的结构相同,在B表新增记录时要检查字段DM的值,如果B表已存在相同DM,则更新B表该行记录为A表对应行的值,如果B表不存在相同DM,则在B表中新增一行A表中的数据。要用触发器怎样写才行。

解决方案 »

  1.   

    update tb set .... --更新字段自己写
    from ta where ta.dm=tb.dm
    insert tb 
    select * 
    from ta t 
    where not exists(select 1 from tb where dm=t.dm)
      

  2.   

    create tigger f on b
    for insert,update
    as
    begin
    insert into b select * from inserted where not exists(select 1 from a where dm=i.dm)
    update b set dm=i.dm from inserted i where  exists(select 1 from a where dm=i.dm)
    end
      

  3.   

    create trigger my_tr on B
    for insert
    as
    update a set 
        a.col1=b.col1,a.col2=b.col2.....
    from ta a
        join tb b
            on a.DM=b.DM
    insert ta 
        select * from inserted t
        where not exists(select 1 from ta where dm=t.dm)
      

  4.   


    create trigger on B表 for insert
    asupdate  b
    set b.col=a.col......
    from insertd a, B表 b 
    where a.dm=b.dminsert B表 
    select * 
    from A表 a 
    where not exists(select 1 from B表 b where a.dm=b.dm)
      

  5.   

    create trigger on_b on b
    instead of insert
    as
    begin 
    delete b 
    where exists(select * from inserted d where b.dm=dm)
    insert b
    select * from inserted
    end
      

  6.   

    可能理解错了.如果B表不存在相同DM,则在B表中新增一行A表中的数据。
    --也就是说,B表在新增记录时,如果DM在A表中没存在,任意取一条A表中的数据插入到B表,是这样吗?
      

  7.   

    create tigger f on b
    for insert,update
    as
    begin
    update b set col=i.col from inserted i,a  where  a.dm=i.dm
    insert into b select * from inserted where not exists(select 1 from a where dm=i.dm)
    end
      

  8.   

    主要楼主没说清楚
    这2表字段不知道相同否 
    还有DM是不是主键。如果B表和A表是一样的表机构 而且DM是主键
    那应该就可以了。
      

  9.   

    我看来看去也是没理解,估计要用instead of 触发器
      

  10.   

    简单的说,就是在表B新增记录时,如果是表B已存在A表相同DM的记录就更新,否则新增一行
      

  11.   


    如果是表B已存在A表相同DM的记录就更新 --更新哪个表?