库存主表:
id(订单id),order_status,flag,pipei
库存明细表
id(订单id),size_id(商品id),amount
库存表
id(商品id),stock_c,sale_count,saleb_c,total
主要想实现,用订单明细表里的每条记录的amount 去匹配库存,如果 如果一张订单的每笔明细都amount<stock_c,则表示整张订单匹配,并要减去库存表的可用数量 stock_c-amount ,如果一张订单只要有一条明细库存不够,则整张订单部用匹配,不修改库存,
现在实现的难点是:某种商品在完成一次匹配之后,可用库存数量就要修改,下一次这种商品的可用库存数量就是新的了,
而用一般的sql语句写,很难保证 stock_c>amount 里的 stock_c 是动态的,
用游标去写的话,速度又太慢,不能接,请高手指点,不用游标,如何去实现这样的功能?
这是我用游标写的,速度实在太慢,请高手帮忙啊,不用游标,如何去实现这样的功能
Declare @Id integer
Declare Cur Cursor For
select top 1 id from hgorder where order_status=10 for read only
Open Cur  
Fetch next From Cur Into @Id
While @@fetch_status=0  
  begin
  update goodstock set sale_count=sale_count+amount,stock_C=total-sale_count-saleB_C
  from (select A.id,size_id,amount,stock_c from ordersgoods A,goodstock B
  where a.id=@id and a.size_Id=b.id and total-sale_count-saleB_C>0) G where goodstock.id=size_Id and G.ID not in
  (select e.id from ordersgoods E,goodstock F  
  where e.id=@id and e.size_Id=f.id and E.amount>total-sale_count-saleB_C )
    
  end
 Close Cur  
 Deallocate Cur  --order_status=10 表示只处理订单状态为10的订单!
 
 
 

解决方案 »

  1.   

    --简单写下,没测试语句declare @orderid varchar(10)
    select @orderid='000001'if (select count(1) from 库存主表 a join 库存明细表 b on a.订单id=b.订单id
                                        join 库存表 c on c.商品id=b.商品id
         where b.amount<=c.stock_c and order_status=10 and a.订单id=@orderid )>0
    begin
    update 
    update 库存表 set sale_count=sale_count+amount,stock_C=total-sale_count-saleB_C
           from 库存明细表 a where a.订单id=@orderid and order_status=10 and a.商品id=库存表.商品id
    end
    else
    begin
    raiserror('库存不足,不能出库',16,1)
    end