Click here to Skip to main content
15,901,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Two tables Stock and Invoice
Stock SR_NO is FK in Invoice

I want to update the table of stock when the invoice table is save

when Quantity enter in the Invoice the Quantity in stock will update

What I have tried:

UPDATE T1
SET T1.Qty = T1.Qty - T2.Qty
FROM Stock T1
INNER JOIN G_Invoice T2
ON T1.SR_NO = T2.SR_NO
Posted
Updated 27-Nov-16 9:07am

If I understand the question correctly, and you want to update the Stock-table regardless how the invoice is updated, I would suggest considering a trigger. See CREATE TRIGGER (Transact-SQL)[^]

With the trigger you could react upon insert/update/delete on invoice and do the necessary operations for other tables such as stock.
 
Share this answer
 
Hi,

If I were you, if possible according to your application's requirements, I would create a stored procedure to make both changes. Inside stored procedure, write a query to update "Invoice table" and then "Stock Table".

This solution, from my view, is cleaner and maintainable (only use one object to contain code) than using triggers, but it's up to you.

The pseudocode would be similar to:

CREATE PROCEDURE YourProcedureName
    @param1 -- Your Params
    ...
    @paramN,
    @Quantity
AS
BEGIN
	SET NOCOUNT ON;

    /* Query to insert into G_Invoice */
    insert into G_Invoice 
    values(@param1,..,@paramN,...,@Quantity) /* Set your values */

    /* Query to update Stock */
    SET T1.Qty = T1.Qty - @Quantity
    FROM Stock T1
    INNER JOIN G_Invoice T2
    ON T1.SR_NO = T2.SR_NO
    where /* Set filters according to params */
	
END
GO

Finally, you can call the stored procedure from your access data layer making use of SQL Commands, Entity Framework, etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900