简体   繁体   中英

Update multiple rows with different values in Oracle

I am trying to update multiple rows using an inner view in oracle.

The select statement for updating this view is:

select count(distinct a.numCount) as numCount, a.accNum as accNum ,
s.unitNum as unitNum 
from tableA a,tableS s  where a.accNum is not null and s.fk_id=
(select id from tableD where sid=a.accNum ) 
group by a.accNum ,s.unitNum ;

Update statement that I am trying is below:

update 
(select count(distinct a.numCount) as numCount, a.accNum as accNum ,
s.unitNum as unitNum 
from tableA a,tableS s  where a.accNum is not null and s.fk_id=
(select id from tableD where sid=a.accNum ) 
group by a.accNum ,s.unitNum ) k
set k.unitNum=k.numCount;

I am trying to update unitNum with value of numCount. The above query is not working when used as a view. Is there another way to update this in Oracle.

Please suggest.

Structure of the tables are as below:

TableA

accNum   numCount
-----------------------
111        1
222        5
333        2
111        1
111        1
222        5
222        2


TableS

fk_id  unitNum 
-----------------------
123        0
768        0
734        0

TableD

ID      sid
-----------------------
123      222
768      111
734      333

Output should be as below:

TableS

fk_id  unitNum 
-----------------------
123        3
768        3
734        1

Please suggest

update tableS s
   set unitNum=
        (select count(distinct a.numCount) as numCount
           from tableA a, tableD d
          where s.fk_id=d.id and d.sid=a.accNum
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM