使用 sql 游标的无限循环 (Oracle 10)

Infinite loop with sql cursor (Oracle 10)

我有一个带有 Cursor 的无限循环:

DECLARE
  CURSOR cursor_ab IS
    SELECT num_ab FROM abonne;
BEGIN
  OPEN cursor_ab;
  LOOP
  FETCH cursor_ab INTO numeroAb;
    dbms_output.put_line(numeroAb);
  END LOOP;
  CLOSE cursor_ab;
END;
/

你有想法吗?

谢谢,

您需要出口: 当 cursor_ab%NOTFOUND;

时退出

hth

前面已经指出,这样写循环时,需要指定退出条件:

DECLARE
   CURSOR cursor_ab IS
      SELECT num_ab FROM abonne;
BEGIN
   OPEN cursor_ab;

   LOOP
      FETCH cursor_ab INTO numeroab;

      EXIT WHEN cursor_ab%NOTFOUND;
      DBMS_OUTPUT.put_line (numeroab);
   END LOOP;

   CLOSE cursor_ab;
END;
/

但是,您可以在 PL/SQL 中通过让循环管理游标来完成同样的事情:

DECLARE
   CURSOR cursor_ab IS
      SELECT num_ab FROM abonne;
BEGIN
   FOR numeroab IN cursor_ab LOOP
      DBMS_OUTPUT.put_line (numeroab.num_ab);
   END LOOP;
END;
/