我有三张表 Good Receive header ,Good Receive Details table 和 stock Table
TB_GoodsReceive_HDR - Header Table
ID SupplierID
1 1
TB_GoodsReceive_DTL - Details Table
ID GR_HDR_ID ItemID WarehouseID ExpiryDate Qty
1 1 1 1 4/4/2012 20
2 1 2 1 4/4/2012 30
TB_Stock - Stock Table
ID ItemID WarehouseID ExpiryDate StockType GR_HDR_ID Qty
1 1 1 4/4/2012 R 1 20
1 2 1 4/4/2012 R 1 30
我收到了项目 1 – 20 数量和项目 2 – 30 数量。我的股票表里也有。有很多场景,但我需要这个场景的指南
现在当用户更新 TB_GoodsReceive_DTL 如下:仅更新项目(ItemID 1 到 ItemID 3)
ID GR_HDR_ID ItemID WarehouseID ExpiryDate Qty
1 1 **3** 1 4/4/2012 20
2 1 2 1 4/4/2012 30
My stock table values should be as follows:
ID ItemID WarehouseID ExpiryDate StockType GR_HDR_ID Qty
1 **3** 1 4/4/2012 R 1 20
1 2 1 4/4/2012 R 1 30
但是我下面的存储过程会插入新行,而不是更新库存主数据中的行。同样,用户可能会更新仓库或到期日期
以下过程执行以下操作:
每当收到货物时,它会检查库存表中的itemid,仓库和到期日,如果存在,它将添加数量,否则它将在库存表中插入新项目。
问题是每当更新 Good Receive 表时(意味着当用户更新 ItemId 或 Expiry Date 或 Warehouse 时)如何更新 stock 表。请各位大神帮帮忙,我的头都破了……
Declare
@ItemID int,
@WareHouseID int,
@Qty int,
@StockType nvarchar(30),
@ExpiryDate datetime,
@IsExistsItem int,
@IsExistsWH int,
@IsExistsExpiryDate int,
@IsGR_HDR_ID int,
@GR_HDR_ID int
set @ItemID=1
set @WareHouseID=2
set @Qty=20
set @StockType='R'
set @GR_HDR_ID=2
set @ExpiryDate = '4/4/2012 12:00:00 AM'
set @IsExistsItem = (select count(ItemID) from TB_Stock_Details where
ItemID=@ItemID)
set @IsExistsWH = (select count(WareHouseID) from TB_Stock_Details where
WareHouseID=@WareHouseID)
set @IsExistsExpiryDate = (select count(ExpiryDate) from TB_Stock_Details where
ExpiryDate=@ExpiryDate)
if(@StockType='R')
begin
IF (@IsExistsItem>0) and (@IsExistsWH>0) and (@IsExistsExpiryDate>0)
BEGIN
UPDATE TB_Stock_Details
SET
Qty =Qty + @Qty
WHERE ItemID = @ItemID and GR_HDR_ID = @GR_HDR_ID
END
ELSE
begin
INSERT INTO TB_Stock_Details(ItemID,WareHouseID,Qty,StockType,ExpiryDate)
VALUES(@ItemID,@WareHouseID,@Qty,@StockType,@ExpiryDate)
end
end