[sql] ORACLE: Updating multiple columns at once

I am trying to update two columns using the same update statement can it be done?

IF V_COUNT = 9 THEN
        UPDATE INVOICE
        SET INV_DISCOUNT = DISC3 * INV_SUBTOTAL
                , INV_TOTAL = INV_SUBTOTAL - INV_DISCOUNT       
        WHERE INV_ID = I_INV_ID;
        DBMS_OUTPUT.PUT_LINE ('YOU QUALIFY FOR A DISCOUNT OF 30%');

The issue is that the INV_TOTAL is not updating, only the inv_discount

DISC3 = 0.3 I.E 30% discount, so what ever the sub_total is will be multiplied by 0.3 and that's the value for INV_discount

INV_TOTAL = sub_total - discount

    INV_ID|INV_DATETIME                  |INV_SUBTOTAL|INV_DISCOUNT|  INV_TOTAL
----------|------------------------------|------------|------------|-----------
       100|14-NOV-12 09.40.06.918000     |        $.00|        $.00|       $.00
       101|18-MAR-12 10.03.00.000000     |        $.00|        $.00|       $.00
       102|18-MAR-12 10.15.00.000000     |        $.00|        $.00|       $.00
       103|18-MAR-12 10.55.00.000000     |      $80.00|       $8.00|     $72.00
       104|18-MAR-12 10.38.00.000000     |        $.00|        $.00|       $.00
       105|12-JUN-12 15.15.00.000000     |        $.00|        $.00|       $.00
       106|06-AUG-12 12.13.00.000000     |        $.00|        $.00|       $.00
       107|04-MAY-12 09.15.00.000000     |        $.00|        $.00|       $.00
       108|29-NOV-12 13.16.00.000000     |      $25.00|       $5.00|     $22.50
       109|18-MAR-12 10.37.00.000000     |      $50.00|      $15.00|     $45.00

108 is suppose to be 20% of 25, the discount amount is correct but the inv_total is not, it should be $20, not $22.50

109 is suppose to be 30% of 50 the discount amount is correct but inv_total should be $35

103 calculates fine, which is 10% discount

This question is related to sql oracle

The answer is


I guess the issue here is that you are updating INV_DISCOUNT and the INV_TOTAL uses the INV_DISCOUNT. so that is the issue here. You can use returning clause of update statement to use the new INV_DISCOUNT and use it to update INV_TOTAL.

this is a generic example let me know if this explains the point i mentioned

CREATE OR REPLACE PROCEDURE SingleRowUpdateReturn
IS
    empName VARCHAR2(50);
    empSalary NUMBER(7,2);      
BEGIN
    UPDATE emp
    SET sal = sal + 1000
    WHERE empno = 7499
    RETURNING ename, sal
    INTO empName, empSalary;

    DBMS_OUTPUT.put_line('Name of Employee: ' || empName);
    DBMS_OUTPUT.put_line('New Salary: ' || empSalary);
END;