表A
字段 索引列   index 
     前庫存量 numPre
     數量     num
     后庫存量 numNext
     類別     type--查詢結果
1   25   2  23  銷售出庫
2   23   6  28  生產入庫
3   29   8  21  銷售出庫
4   20   5  15  銷售出庫
5   16   2  18  生產入庫 銷售出庫是減庫存量 生產入庫是加庫存量
更新后確保前一筆的后庫存量與后一筆的庫存量相同--更新后的結果
1   25   2  23  銷售出庫
2   23   6  29  生產入庫
3   29   8  21  銷售出庫
4   21   5  16  銷售出庫
5   16   2  18  生產入庫 

解决方案 »

  1.   

    現在已經造成這種問題了,如果要更新,SQL語句應該怎麼寫呢
      

  2.   

    你可以用如下语句update a1 set
       numPre = (select top 1 numPre from a order by [index]) + (select sum(case when type = '銷售出庫' then -num else num end
     ) from a where [index]<a1.[index] )
       ,numNext = (select top 1 numPre from a order by [index]) + (select sum(case when type = '銷售出庫' then -num else num end
     ) from a where [index]<=a1.[index] )
    from a a1但是这个语句效率是很低的,用触发器可能能减少一些修改量,但是也很影响性能