I need some help updating the quantity on hand in an inventory table with information retrieved from two other tables.
There are three tables involved: Product, Inventory, and Ingredient. The ingredient table is the junction between product and inventory.
What the stored procedure is attempting to do is add a line item from a customer order (this part works fine) and then update the inventory table to subtract the ingredients used (which are tracked by weight). For example a small pizza would contain a specific amount of dough, cheese, sauce, etc.
What is happening is that the stored procedure is updating each appropriate line in the inventory table, but only by the weight of the last product that was retreived in the "select @weight = weight" line. How do we make sure the inventory quantity is reduced by the correct weight for the product?
Here is the stored procedure we are using:
CREATE procedure sp_AddLineItem2 (@oid int,
@prodcd char(5),
@qty float
)
As
declare @localprice money
declare @weight float
declare @inv int
select @localprice=price from cheesy_product where PK_ProductID = @prodcd
select @weight=weight, @inv=FK_invtID from cheesy_Ingredient where FK_ProductID = @prodcd
Begin
Update cheesy_Inventory set quantity = quantity - @weight where PK_InvtID IN(select
FK_invtID from cheesy_ingredient where FK_productID = @prodcd)
insert into Cheesy_lineitems (FK_ProductID, FK_CustOrderID, quantity, price) values
(@prodcd,@oid, @qty,@localprice)
End
if exists (select * from cheesy_lineitems where FK_CustOrderID = @oid and FK_ProductID = @prodcd)
return 10
else
return -1
There are three tables involved: Product, Inventory, and Ingredient. The ingredient table is the junction between product and inventory.
What the stored procedure is attempting to do is add a line item from a customer order (this part works fine) and then update the inventory table to subtract the ingredients used (which are tracked by weight). For example a small pizza would contain a specific amount of dough, cheese, sauce, etc.
What is happening is that the stored procedure is updating each appropriate line in the inventory table, but only by the weight of the last product that was retreived in the "select @weight = weight" line. How do we make sure the inventory quantity is reduced by the correct weight for the product?
Here is the stored procedure we are using:
CREATE procedure sp_AddLineItem2 (@oid int,
@prodcd char(5),
@qty float
)
As
declare @localprice money
declare @weight float
declare @inv int
select @localprice=price from cheesy_product where PK_ProductID = @prodcd
select @weight=weight, @inv=FK_invtID from cheesy_Ingredient where FK_ProductID = @prodcd
Begin
Update cheesy_Inventory set quantity = quantity - @weight where PK_InvtID IN(select
FK_invtID from cheesy_ingredient where FK_productID = @prodcd)
insert into Cheesy_lineitems (FK_ProductID, FK_CustOrderID, quantity, price) values
(@prodcd,@oid, @qty,@localprice)
End
if exists (select * from cheesy_lineitems where FK_CustOrderID = @oid and FK_ProductID = @prodcd)
return 10
else
return -1