如果主库a里有有个表fx_outlist其主键是identify,外键是outno
现在有个同样属性的目标库b有个表为fx_outlist_2011
假设现在fx_outlist里有10个identify对应着相同的outno
fx_outlist里有主库中的4条同样数据
请问怎么把剩下的6条插入目标库中
insert into FxStorehouse.dbo.Fx_OutList_2011
SELECT * from   Fx_OutList where Fx_OutList.Out_No = @Out_No--(@Out_No就是所说的相同单号)
我这么写肯定不对

解决方案 »

  1.   

    insert into FxStorehouse.dbo.Fx_OutList_2011
    SELECT a.* 
    from Fx_OutList as a
    left join FxStorehouse.dbo.Fx_OutList_2011 as b on a.Out_No=b.Out_No
     where b.Out_No is null
      

  2.   

    兩個表是通過Out_No關聯麼?Out_No在表中需要唯1
      

  3.   

    insert into b..fx_outlist_2011 select * from a..fx_outlist n
    where not exists(select 1 from b..fx_outlist_2011 m where n.outno = m.outno and n.outno = @out_no)
      

  4.   

    --插入所有不存在的outno
    insert into b..fx_outlist_2011 select * from a..fx_outlist n
    where not exists(select 1 from b..fx_outlist_2011 m where n.outno = m.outno)--如果你需要指定outno,则如下:
    insert into b..fx_outlist_2011 select * from a..fx_outlist n
    where not exists(select 1 from b..fx_outlist_2011 m where n.outno = m.outno and n.outno = @outno) --其中@outno是个变量,其值自己输入。
      

  5.   

    不是不存在outno,打个比方a中的同一个outno对应的10个id,b中对应2个,现在我需要的是把a中另外8个b中没有的id加入到b中