有一个一万行数据的表,一个一百万行数据的表,两个表的结构相同,
要把这一万行数据插入到一百万行数据的表中,无则插入,有则更新。
用什么算法?
2种情况:
1)表中的数据是有顺序的
2)表中的数据是无顺序的谢谢

解决方案 »

  1.   

    1、前提是必须有相同的项目,如id主键。
    使用sql server 2008 中的新增语句merge2、若不满足1,则只能通过逐行比较了
      

  2.   

    1。有顺序是否是有默认的规则,如根据规则,不有查100万的表即可知1万的表要插入那些行。
      如果是,就根据规则过滤1万行数据再插入
    2。先试试
     select * from [1万表] as a where not exists(select * from 大表 where 主键=a.主键)
    的查询速度,时间不是很入就用一个语句插入
     
      

  3.   

    insert into tb1
    select * from tb2
    except
    select * from tb1
      

  4.   

    使用临时表,a代表1万,b代表百万,匹配字段为id,#in 代表有的那部分,#out代表没有的部分select a.*  into #in from a inner join b on a.id=b.idselect a.* into #out from a left outer join #in on a.id=b.id where b.id is nullupdate b set b.id=#in.id,b.c1=#in.c1.... from b inner join #in on #in.id=b.id
    insert into b select * from #out
    drop table #in
    drop table #out