Updating database records in a loop?

前端 未结 6 799
轮回少年
轮回少年 2021-02-10 09:19
declare
begin
  for i in (select * from emp)
  loop
    if i.sal=1300 then
      update emp
      set sal=13000;
    end if;
  end loop;
end;

This code

6条回答
  •  清歌不尽
    2021-02-10 09:27

    This code is updating all the records with salary 13000. Instead i want to update records having salary 1300 to the value 13000.

    for every record i am checking the sal value of that record.. if salary value in a particular record is 1500 i want to update it to 15000..

    So what exactly do you want?

    You want to update only 1,500 salary, you issue:

    UPDATE emp
    SET sal = 15000
    WHERE sal = 1500;
    

    You want to increase all salary ten times, you issue:

    UPDATE emp
    SET sal = sal * 10;
    

提交回复
热议问题