简体   繁体   中英

SQL UPDATE within BEGIN and END

I need to update big table (ACCOUNTS) and change column based on itself.

How do do this?

This query:

begin
UPDATE ACCOUNTS SET ACC = '1' where ACC IN ('3');
UPDATE ACCOUNTS SET ACC = '2' where ACC IN ('4');
end

Updates only 4 to 2.

For larger tables it is anyway better to perform only one update .

You may combine both updates in one as follows:

select * from ACCOUNTS;

A
-
1
2
3
4


BEGIN
  update ACCOUNTS
  set ACC = case when ACC = '3' then '1'
                 when ACC = '4' then '2' end
  where ACC in ('3','4'); 
  dbms_output.put_line('rows updated '  || SQL%ROWCOUNT);
END;
/

rows updated 2

select * from ACCOUNTS;

A
-
1
2
1
2

Do not forget to COMMIT .

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