Updating database records in a loop?

前端 未结 6 795
轮回少年
轮回少年 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:34

    You need to put a constraint on your update statement.

    What you have at the moment will loop through the results rows, and if it finds a row with salary equal to 1300, if then executest he following SQL:

    update emp 
    set sal=13000;

    Without the contraint this updates every row.

提交回复
热议问题