Szukaj…
Składnia
- PĘTLA
- [sprawozdania];
- EXIT WHEN [warunek dla pętli wyjściowej];
- PĘTLA KOŃCOWA;
Prosta pętla
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 Loop
Pętla WHILE jest wykonywana do momentu spełnienia warunku zakończenia. Prosty przykład:
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;
Ta pętla będzie wykonywana, dopóki aktualna wartość zmiennej v_counter nie będzie mniejsza niż dziesięć.
Wynik:
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
Najważniejsze jest to, że nasza pętla zaczyna się od wartości „0”, więc pierwszą linią wyników jest „Bieżąca iteracja pętli wynosi 0”.
Dla pętli
Pętla FOR działa na podobnych zasadach jak inne pętle. Pętla FOR jest wykonywana dokładnie tyle razy, a liczba ta jest znana na początku - dolne i górne limity są bezpośrednio ustawione w kodzie. Na każdym kroku w tym przykładzie pętla jest zwiększana o 1.
Prosty przykład:
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;
Rezultat to:
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
Pętla FOR ma dodatkową właściwość, która działa odwrotnie. Używaj dodatkowego słowa „REVERSE” w deklaracji dolnej i górnej granicy pętli. Każde wykonanie wartości zmniejszenia liczby pętli licznika v_ o 1.
Przykład:
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;
A wynik:
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