只要您可以使用一条语句进行更新,就应该这样做,而不要使用循环。这样,您将获得巨大的性能提升。或者反过来,循环更新会浪费很多性能。
如果确实需要使用循环,那么当然需要一个where条件,以确保仅更新您真正想要更新的记录。始终有效的一种可能方法(即使没有可用的唯一键)是使用rowid伪列:
begin
for i in (select rowid, emp.* from emp)
loop
if i.sal=1300 then
update emp
set sal=13000
where rowid=i.rowid;
end if;
end loop;
end;
另一种可能是使用显式游标和“ update ... where current of cursorname ”语法。