I have an invoice which is stored in two tables, first table has the general information of the invoice like invoice number,date,and so on .. And second table has the details of items purchased in the invoice like item name,quantity, and so on...
I want to create a stored procedure to first insert the general information of the invoice then loop through the items of the invoice which will be in datagridview rows to insert them one by one in the invoice details table.And in each detail insertion the quantity of the items in inventory are updated so this making my stored procedure handles 3 sql queries.
I've written a stored procedure but I'm stuck in the part of writing the loop statement in the stored procedure it self .. Can any one help!
Create proc [dbo].[AddNewSellDetails]
@invoiceNo int,
@invoice_date date,
@SELL_ID int,
@PRO_ID int,
@QUANTITY varchar(10),
@UNIT nvarchar(15),
@PRICE varchar(20),
@AMOUNT varchar(20),
@DISCOUNT varchar(20),
@FINAL_TOT varchar(30)
as
begin transaction
begin try
INSERT INTO Sell
(ID,sellDate) values (@invoiceNo,@invoice_date)
INSERT INTO Sell_Details
(Sell_ID
,Product_ID
,Quantity
,Unit
,Price
,Amount
,Discount
,Final_Tot)
VALUES
(@SELL_ID
,@PRO_ID
,@QUANTITY
,@UNIT
,@PRICE
,@AMOUNT
,@DISCOUNT
,@FINAL_TOT)
update Product set Product.Quantity = Product.Quantity - CONVERT(float,@QUANTITY)
where Product.Product_ID=@PRO_ID
commit transaction
end try
begin catch
rollback transaction
end catch