Below is a stored procedure named prc_inv_amounts that updates the INV_SUBTOTAL, INV_TAX, and INV_TOTAL based on the provided invoice number:
The Procedure
CREATE PROCEDURE prc_inv_amounts invoice_number INT
AS
BEGIN
UPDATE Invoices
SET
INV_SUBTOTAL = (
SELECT SUM(LINE_TOTAL)
FROM Invoice_Lines
WHERE Invoice_Lines.INVOICE_NUM = invoice_number
),
INV_TAX = (
SELECT SUM(LINE_TOTAL) * 0.08
FROM Invoice_Lines
WHERE Invoice_Lines.INVOICE_NUM = invoice_number
),
INV_TOTAL = (
SELECT SUM(LINE_TOTAL) + SUM(LINE_TOTAL) * 0.08
FROM Invoice_Lines
WHERE Invoice_Lines.INVOICE_NUM = invoice_number
)
WHERE INVOICE_NUM = invoice_number
END;
This stored procedure calculates and updates the INV_SUBTOTAL, INV_TAX (at 8% rate), and INV_TOTAL for a specified invoice number by summing the LINE_TOTAL amounts from the Invoice_Lines table.