How to update the records of multiple tables of a database,while modifying in any one table of that database
I am creating a C# Windows application which is based on a medical 开发者_运维知识库inventory.In this application I have mainly three forms as PurchaseDetail,SalesDetail,and StockDetail.
Now I want a functionality in which if I insert or modify the records in PurchaseDetail,or SalesDetail, the data in the StockDetail should also be modified.(for example if i insert some quantity of medicines in PurchaseDetail then Quantity of that medicine in StockDetail should also modified and same as for SalesDetail )
Columns in PurchaseDetail: Id(Primary Key and auto increment int),BatchNumber,ProductName,ManufacturingDate,ExpiryDate,Rate,MRP,Tax,Discount,Quantity
Columns in SalesDetail: Id(PrimaryKey and auto increment int),BillNumber,CustomerName,BatchNumber,Quantity,Rate,SalesDate
Columns in StockDetail: Id(Primary Key and auto increment int),ProductId,ProductName,OpeningStock,ClosingStock,PurchaseQty,DispenseQty,PurchaseReturn,DispenseReturn
Please help me with any suitable example or code
you could easily write multiple SQL statements separated by semicolon ";"
example:
INSERT INTO PurchaseDetail (BatchNumber, ProductID, Quantity) VALUES (@BatchNumber, @ProductID, @Quantity);
INSERT INTO StockDetail (ProductID, ProductName, OpeningStock, ClosingStock, PurchaseQty, DispenseQty, PurchaseReturn,DispenseReturn) SELECT ProductID, ProductName, ClosingStock, ClosingStock + @Quantity, @Quantity, 0, 0, 0 FROM StockDetail WHERE ProductID = @ProductID AND ID = (SELECT TOP 1 ID FROM StockDetail WHERE ProductID = @ProductID ORDER BY ID)
And you really should not be storing the product name all over again in each of the table. Instead, you should be storing ProductID. Your SalesDetails doesn't say which product is sold.
The following is a suggested schema that is more normalized:
Product (ID, ProductName, Stock) PurchaseDetail (ID, BatchID, ProductID, Quantity, ...) SalesDetail (ID, BillID, ProductID, Quantity, ...) PurchaseBatch (ID, PurchaseDate) SalesBill (ID, SalesDate, CustomerID)
If you ever need to know the opening, closing, purchase and dispense for a specific duration, you can always run the SQL:
Current stock:
SELECT Stock FROM Product WHERE ID = @ProductID
Purchased from @StartDate to @EndDate:
SELECT SUM(Quantity) FROM PurchaseDetail INNER JOIN PurchaseBatch ON PurchaseDetail.BatchID = PurchaseBatch.ID WHERE PurchaseBatch.PurchaseDate >= @StartDate AND PurchaseBatch.PurchaseDate < @EndDate AND PurchaseDetail.ProductID = @ProductID
Sold from @StartDate to @EndDate:
SELECT SUM(Quantity) FROM SalesDetail INNER JOIN SalesBill ON SalesDetail.BillID = SalesBill.ID WHERE SalesBill.SalesDate >= @StartDate AND SalesBill.SalesDate < @EndDate AND SalesDetail.ProductID = @ProductID
Purchased from @EndDate till now:
SELECT SUM(Quantity) FROM PurchaseDetail INNER JOIN PurchaseBatch ON PurchaseDetail.BatchID = PurchaseBatch.ID WHERE PurchaseBatch.PurchaseDate >= @EndDate AND PurchaseDetail.ProductID = @ProductID
Sold from @EndDate till now:
SELECT SUM(Quantity) FROM SalesDetail INNER JOIN SalesBill ON SalesDetail.BillID = SalesBill.ID WHERE SalesBill.SalesDate > @EndDate AND SalesDetail.ProductID = @ProductID
Therefore,
opening from @StartDate till @EndDate = Current stock - Purchased from @StartDate to @EndDate - Purchased from @EndDate till now + Sales from @StartDate to @EndDate + Sales from @EndDate till now
closing from @StartDate till @EndDate = Current stock - Purchased from @EndDate till now + Sales from @EndDate till now
And you also have Sales from @StartDate to @EndDate Purchased from @StartDate to @EndDate
Try using Triggers.
精彩评论