表结构如下,
CKBM,SPBM。SCRQ,PH,DJ,SL,JE
01    0001  NULL      5    20  100
01    0001   NULL      6    30   180
01    0001  NULL      5     30   150更新数据要求:以CKBM,SPBM。SCRQ,DJ,PH 为条件,SL是60
先更新第一条,如第一条中的SL比60大可以直接更新了,如不大用更新第一条后余下的40来和第二条的比对,依次类推

解决方案 »

  1.   


    if object_id('ta') is not null
       drop table ta
    go
    create table ta
    (
     id int identity(1,1),
     num int not null
    )
    go
    insert into ta (num)
    select 10 union all
    select 20 union all
    select 30 --要减的库存数为40
    update ta set num = case when (select sum(num) from ta where id<=a.id)-40<0 then 0 else (select sum(num) from ta where id<=a.id)-40 end from ta a
      

  2.   


    var
      ASum :Integer;
    begin
      ASum := 100; //出库总数
      ADOQuery1.First;
      while not ADOQuery1.Eof do
      begin
        if True then         //如果符合出库条件
        begin
          if ADOQuery1.FieldByName('SL').AsInteger < ASum then
          begin
            ASum := ASum - ADOQuery1.FieldByName('SL').AsInteger;
            ADOQuery1.Edit;
            ADOQuery1.FieldByName('SL').AsInteger := 0;          //更改库存
            ADOQuery1.Post;
            ADOQuery1.Next;
          end
          else
          begin
            ADOQuery1.Edit;
            ADOQuery1.FieldByName('SL').AsInteger := ADOQuery1.FieldByName('SL').AsInteger - ASum;   //更改库存
            ADOQuery1.Post;
            Exit;
          end;
        end
        else
          ADOQuery1.Next;
      end;
    end;