解决方案 »

  1.   

    delete ordd where id in (
    select d.id
    from ordd d 
    join Invh b on d.InvNum = b.InvNum 
    where b.Period = '201405' --发票月份
    and (isnull(d.invnum,'')<>'' or d.Qty1<0)  and d.LPNum<>'NOLP' --发票不为空,如果发票为空,必须QTY1为负数,即为退货。且Lpnum不等于’NOLP‘
    and (isnull(d.lpnum,'')<>'' or d.Qty1<0) ----Lpnum不为空,如果Lpnum为空,必须QTY1为负数,即为退货
    )id是key,其他以此类推
      

  2.   

    delete  invh i where i.Period = '201405'  --发票月份 
      

  3.   


    delete from ordd
    from ordd d 
    join Invh b on d.InvNum = b.InvNum 
    where b.Period = '201405' --发票月份
    and (isnull(d.invnum,'')<>'' or d.Qty1<0)  and d.LPNum<>'NOLP' --发票不为空,如果发票为空,必须QTY1为负数,即为退货。且Lpnum不等于’NOLP‘
    and (isnull(d.lpnum,'')<>'' or d.Qty1<0) ----Lpnum不为空,如果Lpnum为空,必须QTY1为负数,即为退货
    ---ordd
      

  4.   

    delete  Shipment s left join invh i on i.ShpNum = s.ShpNum  where i.Period = '201405' 
      

  5.   

    delete ordd where id in (
    select d.id
    from ordd d 
    join Invh b on d.InvNum = b.InvNum 
    where b.Period = '201405' --发票月份
    and (isnull(d.invnum,'')<>'' or d.Qty1<0)  and d.LPNum<>'NOLP' --发票不为空,如果发票为空,必须QTY1为负数,即为退货。且Lpnum不等于’NOLP‘
    and (isnull(d.lpnum,'')<>'' or d.Qty1<0) ----Lpnum不为空,如果Lpnum为空,必须QTY1为负数,即为退货
    )
      

  6.   

    按顺序进行,不然下面无法找到表的数据
    delete LP where ID in ( 
    select ID 
    from LP l
    join 
    (select d.lpnum from ordd d
    left join ordh h on d.SONum = h.SONum 
    left join invh i on d.InvNum=i.InvNum 
    where Period = '201405' --发票月份
    and (isnull(d.lpnum,'')<>'' or d.Qty1<0) ----Lpnum不为空,如果Lpnum为空,必须QTY1为负数,即为退货
    and (isnull(d.invnum,'')<>'' or d.Qty1<0) and d.LPNum<>'NOLP'  --发票不为空,如果发票为空,必须QTY1为负数,即为退货。且Lpnum不等于’NOLP‘
    group by d.lpnum
    )d on l.LPNum = d.LPNum)  --lpdelete ordh where ID  in (
    select  ID  from ordh h
    right join 
    (select d.sonum from ordd d 
    left join Invh i on d.InvNum = i.InvNum 
    where i.Period = '201405' --发票月份
    and (isnull(d.invnum,'')<>'' or d.Qty1<0)  and d.LPNum<>'NOLP' --发票不为空,如果发票为空,必须QTY1为负数,即为退货。且Lpnum不等于’NOLP‘
    and (isnull(d.lpnum,'')<>'' or d.Qty1<0) ----Lpnum不为空,如果Lpnum为空,必须QTY1为负数,即为退货
    group by d.sonum )
     d on h.SONum = d.SONum) --ordh  
    delete ordd  where ID  in
    (select ID 
    from ordd d 
    join Invh b on d.InvNum = b.InvNum 
    where b.Period = '201405' --发票月份
    and (isnull(d.invnum,'')<>'' or d.Qty1<0)  and d.LPNum<>'NOLP' --发票不为空,如果发票为空,必须QTY1为负数,即为退货。且Lpnum不等于’NOLP‘
    and (isnull(d.lpnum,'')<>'' or d.Qty1<0) ----Lpnum不为空,如果Lpnum为空,必须QTY1为负数,即为退货
    ---ordd
    )delete Shipment where ID in (
    select  ID  from Shipment s
    left join invh i on i.ShpNum = s.ShpNum 
    where i.Period = '201405') --发票月份
    --shipmentdelete  from invh where Period = '201405'  --发票月份