我尝试在我的库存表上使用FIFO方法,其中4列为项目代码,制造日期,库存数量,订单数量
因此,当我收到某项目的订单时,“订购数量”列将被更新,并且应根据最早的MFG日期进行处理,一旦库存用完,它应移至下一个MFG日期。如何在SQL中完成此操作。
也许这可以解决您的问题。如果不是,请详细解释您的情况
进入实际实现之前,请参考链接How to Loop through the set of records和Cursor
库存表的样本数据:
查询FIFO基础
Declare @ReqOrderQty as int;
Set @ReqOrderQty=45; // Requested Qty to Update the Records
Declare
@ItemNo Varchar(10),
@MFGCode Varchar(10),
@StockQty int,
@OrderQty int;
Declare @Query Varchar(500);
Declare Records Cursor
for Select ItemNo,MFGCode,StockQty,OrderQty from Inventory where ItemNo='1' and OrderQty <> StockQty order by MFGCode asc
OPEN Records
FETCH NEXT FROM Records INTO
@ItemNo,
@MFGCode,
@StockQty,
@OrderQty;
WHILE @@FETCH_STATUS = 0
BEGIN
IF @ReqOrderQty > @StockQty
BEGIN
Set @ReqOrderQty = @ReqOrderQty - @StockQty;
Set @Query='Update Inventory set OrderQty=' +CAST(@StockQty as varchar(100))+' where ItemNo='''+@ItemNo +'''and MFGCode='''+@MFGCode+''''
END
Else
BEGIN
Set @ReqOrderQty = @ReqOrderQty % @StockQty;
Set @Query='Update Inventory set OrderQty=' +CAST(@ReqOrderQty as varchar(100))+' where ItemNo='''+@ItemNo +'''and MFGCode='''+@MFGCode+''''
END
PRINT @Query
Exec (@Query)
FETCH NEXT FROM Records INTO
@ItemNo,
@MFGCode,
@StockQty,
@OrderQty;
END;
CLOSE Records;
DEALLOCATE Records;
输出
字符串拆分
create table #Temp(value varchar(10))
Declare @ReqOrderQty Varchar(200)
Set @ReqOrderQty = '200,40,10,100,150';
INSERT INTO #Temp SELECT * FROM STRING_SPLIT ( @ReqOrderQty , ',' )
// Perform the Cursor Operation as mentioned above
Drop Table #Temp
接受答案,如果有帮助的话