Sök…
Syntax
- SLINGA
- [uttalanden];
- EXIT NÄR [villkor för utgångslinga];
- END LOOP;
Enkel slinga
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
WHILE-slingan körs tills villkoret för slut är uppfyllt. Enkelt exempel:
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;
Denna slinga kommer att köras tills det aktuella värdet för variabel v_counter är mindre än tio.
Resultatet:
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
Det viktigaste är att vår slinga börjar med '0' -värde, så första resultatraden är 'Aktuell iteration av slinga är 0'.
FÖR ögla
Loop FOR fungerar enligt liknande regler som andra slingor. FOR-loop utförs exakt antal gånger och detta nummer är känt i början - nedre och övre gränser ställs direkt in i kod. I varje steg i detta exempel ökas slingan med 1.
Enkelt exempel:
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;
Och resultatet är:
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 har ytterligare egendom som fungerar i omvänd riktning. Att använda ytterligare ord "REVERSE" i deklaration av den nedre och övre gränsen för slingan gör det möjligt. Varje exekvering av slingreduktionsvärdet för v_counter med 1.
Exempel:
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;
Och resultatet:
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