Suche…


Syntax

  1. SCHLEIFE
  2. [aussagen];
  3. EXIT WHEN [Bedingung für Exit-Schleife];
  4. END LOOP;

Einfache Schleife

DECLARE
v_counter NUMBER(2);

BEGIN
  v_counter := 0;
  LOOP
    v_counter := v_counter + 1;
    dbms_output.put_line('Line number' || v_counter);
    
    EXIT WHEN v_counter = 10;
  END LOOP;
END;

While-Schleife

Die WHILE-Schleife wird ausgeführt, bis die Endebedingung erfüllt ist. Einfaches Beispiel:

DECLARE
v_counter NUMBER(2); --declaration of counter variable

BEGIN
  v_counter := 0; --point of start, first value of our iteration
 
  WHILE v_counter < 10 LOOP --exit condition
    
    dbms_output.put_line('Current iteration of loop is ' || v_counter); --show current iteration number in dbms script output
    v_counter := v_counter + 1; --incrementation of counter value, very important step

  END LOOP; --end of loop declaration
END;

Diese Schleife wird ausgeführt, bis der aktuelle Wert der Variablen v_counter kleiner als zehn ist.

Das Ergebnis:

Current iteration of loop is 0
Current iteration of loop is 1
Current iteration of loop is 2
Current iteration of loop is 3
Current iteration of loop is 4
Current iteration of loop is 5
Current iteration of loop is 6
Current iteration of loop is 7
Current iteration of loop is 8
Current iteration of loop is 9

Das Wichtigste ist, dass unsere Schleife mit dem Wert '0' beginnt. Die erste Zeile der Ergebnisse lautet 'Aktuelle Iteration der Schleife ist 0'.

FOR-Schleife

Loop FOR arbeitet mit ähnlichen Regeln wie andere Loops. Die FOR-Schleife wird genau so oft ausgeführt und diese Anzahl ist zu Beginn bekannt - untere und obere Grenzen werden direkt im Code festgelegt. In jedem Schritt dieses Beispiels wird die Schleife um 1 erhöht.

Einfaches Beispiel:

DECLARE
v_counter NUMBER(2); --declaration of counter variable

BEGIN
  v_counter := 0; --point of start, first value of our iteration, execute of variable
 
  FOR v_counter IN 1..10 LOOP --The point, where lower and upper point of loop statement is declared - in this example, loop will be executed 10 times, start with value of 1
    
    dbms_output.put_line('Current iteration of loop is ' || v_counter); --show current iteration number in dbms script output
   
  END LOOP; --end of loop declaration
END;

Und das Ergebnis ist:

Current iteration of loop is 1
Current iteration of loop is 2
Current iteration of loop is 3
Current iteration of loop is 4
Current iteration of loop is 5
Current iteration of loop is 6
Current iteration of loop is 7
Current iteration of loop is 8
Current iteration of loop is 9
Current iteration of loop is 10

Loop FOR hat eine zusätzliche Eigenschaft, die umgekehrt arbeitet. Verwenden Sie das Zusatzwort 'REVERSE' in der Deklaration der unteren und oberen Schleifengrenze, um dies zu ermöglichen. Jede Ausführung des Schleifendekrementierungswerts von v_counter um 1.

Beispiel:

DECLARE
v_counter NUMBER(2); --declaration of counter variable

BEGIN
  v_counter := 0; --point of start
 
  FOR v_counter IN REVERSE 1..10 LOOP
    
    dbms_output.put_line('Current iteration of loop is ' || v_counter); --show current iteration number in dbms script output
   
  END LOOP; --end of loop declaration
END;

Und das Ergebnis:

Current iteration of loop is 10
Current iteration of loop is 9
Current iteration of loop is 8
Current iteration of loop is 7
Current iteration of loop is 6
Current iteration of loop is 5
Current iteration of loop is 4
Current iteration of loop is 3
Current iteration of loop is 2
Current iteration of loop is 1


Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow