user2931656
user2931656

Reputation: 253

Infinite loop with sql cursor (Oracle 10)

I have an infinite loop with 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;
/

Do you have an idea ?

Thank,

Upvotes: 1

Views: 2050

Answers (2)

Allan
Allan

Reputation: 17429

As has already been pointed out, when writing a loop this way, you need to specify the exit condition:

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;
/

However, you can accomplish the same thing in PL/SQL by letting the loop manage the cursor:

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;
/

Upvotes: 4

Younes
Younes

Reputation: 395

You need the exit: EXIT WHEN cursor_ab%NOTFOUND;

hth

Upvotes: 3

Related Questions