insert into inv120(partdate,part) select getdate(),part from inv100
如上语句,是将inv100表中的数据添加到inv120中,
我想验证,如果日期(yyyy-MM-dd,只看日期,小时分钟不计)和 part在inv120表中已经存在,就不添加,
下面代码没有解决。提示(0 行受影响)insert into inv120(partdate,part) select getdate(),part from inv100 t
where not exists(select 1 from inv120 i
where convert(varchar(10),i.partdate,120)=convert(varchar(10),getdate(),120))我想要的判断是:
如果有1条存在,这1条不添加,其他的还是添加的。
如果有3条存在,这3条不添加,其他的还是添加的。
现在的情况是,只要有一条存在,其他的都不添加。这样不对。

解决方案 »

  1.   

    例如:inv120中的数据有
    2010-03-26  123   
    2010-03-26  124   inv100中的数据有:
    2010-03-26  121
    2010-03-26  122
    2010-03-26  123
    2010-03-26  124
    2010-03-26  125
    2010-03-26  126执行批量添加语句,将inv100中的数据全部添加到inv120,其中要验证。
    得到的最后结果是:
    inv120执行批量添加后的数据:
    2010-03-26  121
    2010-03-26  122
    2010-03-26  123
    2010-03-26  124
    2010-03-26  125
    2010-03-26  126
      

  2.   


    insert into inv120(partdate,part) select getdate(),part from inv100 t
    where 主键 not in (select inv100.主键 from inv100,inv120 where inv100.时间=inv120.时间 and inv100.part = inv120.part)你试下,将条件给改一下
    主要就是先判断把相同的数据剔除掉 再插入。。
      

  3.   

    insert into inv120(partdate,part) select getdate(),part from inv100 t
    where not exists(select 1 from inv120 i
    where convert(varchar(10),i.partdate,120)=convert(varchar(10),getdate(),120)  and a.part =b.part)加个条件
    and a.part =b.part