怎样实现已经插入的数据不要再重复插入,只插入inv1表中新增的数据插入到临时表yptc
----插入到临时表中
select T0.itemcode ,T0.Dscription ,T0.Whscode,T2.suppcatnum,T0.Quantity,T0.Priceafvat,T2.[LstEvlPric],T0.Quantity*T0.PriceafVat as 'price1' ,T2.[LstEvlPric]*T0.Quantity as 'cost'  ,(T0.Quantity*T0.PriceafVat)-(T2.[LstEvlPric]*T0.Quantity) as'cost1',T0.U_003,T0.U_004,(T0.Quantity*T0.PriceafVat-T2.[LstEvlPric]*T0.Quantity)*T0.U_004 as 'total2',T0.DocEntry,T0.U_001,T0.U_002,T0.U_005,T0.U_008, T1.U_DocDate into yptc from inv1 T0 inner join oinv T1 on T0.DocEntry=T1.DocEntry inner join oitm T2 on  T0.itemcode =T2.itemcode
 and T0.U_004='0.08' and T0.itemcode<>'1000001' and T0.itemcode<>'40188888'
and CAST(T1.U_DocDate AS datetime) between '2010/05/19' and '2011/12/31'把此查询写在了作业中,在某个特定的时间执行,如何实现。

解决方案 »

  1.   

    加个 not exists 就行了----插入到临时表中
    insert into yptc 列1,列2,................................
    select T0.itemcode ,T0.Dscription ,T0.Whscode,T2.suppcatnum,T0.Quantity,T0.Priceafvat,T2.[LstEvlPric],T0.Quantity*T0.PriceafVat as 'price1' ,T2.[LstEvlPric]*T0.Quantity as 'cost' ,(T0.Quantity*T0.PriceafVat)-(T2.[LstEvlPric]*T0.Quantity) as'cost1',T0.U_003,T0.U_004,(T0.Quantity*T0.PriceafVat-T2.[LstEvlPric]*T0.Quantity)*T0.U_004 as 'total2',T0.DocEntry,T0.U_001,T0.U_002,T0.U_005,T0.U_008, T1.U_DocDate into yptc from inv1 T0 inner join oinv T1 on T0.DocEntry=T1.DocEntry inner join oitm T2 on T0.itemcode =T2.itemcode
     and T0.U_004='0.08' and T0.itemcode<>'1000001' and T0.itemcode<>'40188888'
    and CAST(T1.U_DocDate AS datetime) between '2010/05/19' and '2011/12/31'
    and not exists(select 1 from yptc b where b.主键 = T0.主键)
      

  2.   

    写成一个存储过程 在调用的时候判断下 
    if exists(select 1 from tb where ...)
    begin
    ...
    end
      

  3.   

    一般如果我遇到这样的问题会喜欢在inv1表中加入一个判断字段表示是否新插入的记录,然后在作业里对已经插入的数据,执行更新这个表inv1。。我想思路上会清晰很多,