It's perfectly possible to update multiple columns in the same statement, and in fact your code is doing it. So why does it seem that "INV_TOTAL is not updating, only the inv_discount"?
Because you're updating INV_TOTAL with INV_DISCOUNT, and the database is going to use the existing value of INV_DISCOUNT and not the one you change it to. So I'm afraid what you need to do is this:
UPDATE INVOICE
SET INV_DISCOUNT = DISC3 * INV_SUBTOTAL
, INV_TOTAL = INV_SUBTOTAL - (DISC3 * INV_SUBTOTAL)
WHERE INV_ID = I_INV_ID;
Perhaps that seems a bit clunky to you. It is, but the problem lies in your data model. Storing derivable values in the table, rather than deriving when needed, rarely leads to elegant SQL.
Answer from APC on Stack OverflowIt's perfectly possible to update multiple columns in the same statement, and in fact your code is doing it. So why does it seem that "INV_TOTAL is not updating, only the inv_discount"?
Because you're updating INV_TOTAL with INV_DISCOUNT, and the database is going to use the existing value of INV_DISCOUNT and not the one you change it to. So I'm afraid what you need to do is this:
UPDATE INVOICE
SET INV_DISCOUNT = DISC3 * INV_SUBTOTAL
, INV_TOTAL = INV_SUBTOTAL - (DISC3 * INV_SUBTOTAL)
WHERE INV_ID = I_INV_ID;
Perhaps that seems a bit clunky to you. It is, but the problem lies in your data model. Storing derivable values in the table, rather than deriving when needed, rarely leads to elegant SQL.
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;
sql - how to update multiple rows in oracle - Stack Overflow
oracle - Updating Multiple Rows with Data from Column A into Column B? - Database Administrators Stack Exchange
How To update multiple rows at the same time in SQL
How to update multiple columns in the same table with the same sub-query in Oracle SQL - Stack Overflow
You should be able to use MERGE statement to do it in a single shot. However, the statement is going to be rather large:
MERGE INTO employee e
USING (
SELECT 1 as d_id, 'cd234' as staff_no FROM Dual
UNION ALL
SELECT 2 as d_id, 'ef345' as staff_no FROM Dual
UNION ALL
SELECT 3 as d_id, 'fg456' as staff_no FROM Dual
UNION ALL
... -- More selects go here
SELECT 200 as d_id, 'za978' as staff_no FROM Dual
) s
ON (e.depno = S.d_id)
WHEN MATCHED THEN UPDATE SET e.staff_no= s.staff_no
use a case expression
UPDATE employee
SET staff_no =
CASE depno
WHEN 1 THEN 'ab123'
WHEN 2 THEN 'ab321'
--...
ELSE staff_no
END
WHERE depno IN ( 1, 2 ) -- list all cases here. use a subquery if you don't want to / cannot enumerate
If you just want the discharge_dt to be overwritten with the admit_dt from the same row, it sounds like you just need
UPDATE patient
SET discharge_dt = admit_dt
WHERE facility_id = 'X'
AND pat_seq IN (<<query that returns the 60,000 keys>>)
Cloyd, the description is indeed confusing.
I'll state some assumptions and work on them. Please, shall you consider this worth, comment out and I'll keep editing the answer until we get 'there'. Deal?
Let's go:
the
updatestatement has as conditionfacility_id='X'the sub-query at the assignment has no condition at all (except
pat_seq)therefore, I'm assuming and 'forecasting' that:
pat_seqis key for identification of a patientboth
admit_dtanddischarge_dtare of type date/time, not just datethe
facility_idcolumn atpatienttable keeps track of the several facilities within which the patient has been sent during it's (sad?) stayfor any patient, for each facility where it's been sent to, there has to be a row keeping track of date/time of admission and discharging, like:
pat_seq: 0015, facility_id: AABB, admit_dt: 15:35, discharge_dt:16:45pat_seq: 0015, facility_id: CCA1, admit_dt:16:45, discharge_dt:17:10pat_seq: 0015, facility_id: AAHD, admit_dt:17:10, discharge_dt: NULL
just the admitances are registered, hence for any new admitance registered there has to be an update action upon the imediate previous entry for which the discharge time will be assumed as the admitance time within the next facility
bullseye?
Well, for this scenario, you shall do, assuming a batch, scheduled task:
UPDATE patient
SET discharge_dt = (
/*
For every entry past this, retrieve the least admit_dt, which shall identify the moment the pat_seq moved into the next facility
*/
SELECT MIN(admit_dt)
FROM patient pat_next_admit
WHERE pat_next_admit.pat_seq = patient.pat_seq
AND pat_next_admit.admit_dt > patient.admit_dt
)
WHERE discharge_dt IS NULL /* No need to process rows already processed! */
| Id | Name | Price |
|---|---|---|
| 1 | Pen | 50 |
| 2 | Pencil | 60 |
This is my table structure now I want to update the price of Pen and Pencil at the same time with one SQL query