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
Answer from Sergey Kalinichenko on Stack OverflowYou 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! */
SQL command for UPDATE of multiple rows - Oracle Forums
sql - Update multiple rows using CASE WHEN - ORACLE - Stack Overflow
Update multiple rows using select statement in Oracle - Database Administrators Stack Exchange
sql - Update multiple rows using select statements - Stack Overflow
| 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
Ok based on the fiddle you have given i have tried these and it worked for me
create table account( account_id number primary key,
account_status varchar2(30));
insert into account values(1, '5');
insert into account values(2, '3');
insert into account values(3, '2');
select * from account
update account
set account_status= case
when account_id=1 then '2'
when account_id=2 then '5'
when account_id=3 then '3'
END
select * from account
I didn't use the where condition
try the following
update account
set account_status = CASE account_id
WHEN 004460721 then 2
WHEN 042056291 THEN 5
WHEN 601272065 THEN 3
END
WHERE account_id IN (004460721, 042056291, 601272065 )
;
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.
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;
UPDATE T
SET Size = CASE SKU
WHEN 'A' THEN 20
WHEN 'B' THEN 10
WHEN 'C' THEN 30
WHEN ...
END
Or there may be a formula for calculating the size, but you've failed to give it in your question (Or we may have to switch to a more complex CASE expression, but again, too little detail in the question).
Create a table with the mapping of SKU to new size; update the master table from that.
Many dialects of SQL have a notation for doing updates via joined tables. Some do not. This will work where there is no such notation:
CREATE TABLE SKU_Size_Map
(
SKU CHAR(16) NOT NULL,
Size INTEGER NOT NULL
);
...Populate this table with the SKU values to be set...
...You must have such a list...
UPDATE MasterTable
SET Size = (SELECT Size FROM SKU_Size_Map
WHERE MasterTable.SKU = SKU_Size_Map.Size)
WHERE SKU IN (SELECT SKU FROM SKU_Size_Map);
The main WHERE condition is need to avoid setting the size to null where there is no matching row.
You can probably also do it with a MERGE statement. But the key insight for any of these notations is that you need a table to do the mapping between SKU and size. You either need a table or you need an algorithm, and the sample data doesn't suggest an algorithm.