IF EXISTS (
SELECT
*
FROM
sysobjects
WHERE
name = 'pro_reduce_stock'
) DROP PROCEDURE pro_reduce_stock
GO
CREATE PROCEDURE pro_reduce_stock
@wareHouseId bigint,
@detailId bigint,
@detailNum INT
AS
BEGIN
BEGIN TRANSACTION
BEGIN try
--扣減物料
CREATE TABLE #bomTemp
(
id INT IDENTITY(1,1),
product_detail_id bigint,
item_id bigint,
item_num INT
)
INSERT #bomTemp(product_detail_id,item_id,item_num) SELECT product_detail_id,item_id,item_num FROM op_product_bom WHERE product_detail_id=@detailId
DECLARE @currentIndex INT
DECLARE @totalRows INT
SELECT @currentIndex=1
SELECT @totalRows=COUNT(1) FROM #bomTemp
DECLARE @materielId INT
DECLARE @materielNum INT
SELECT @materielId=0
SELECT @materielNum=0
WHILE(@currentIndex<=@totalRows)
BEGIN
SELECT @materielId=item_id FROM #bomTemp WHERE id=@currentIndex
SELECT @materielNum=item_num FROM #bomTemp WHERE id=@currentIndex
--UPDATE scm_stock_num SET num=num-@detailNum*@materielNum WHERE stock_id=@wareHouseId AND item_id=@materielId
SELECT @currentIndex=@currentIndex+1;
END
--扣減商品明細
--UPDATE scm_stock_num SET num=num-@detailNum WHERE stock_id=@wareHouseId AND product_detail_id=@detailId
COMMIT TRANSACTION
END try
BEGIN catch
ROLLBACK TRANSACTION
END catch
END