WHILE Loop
執行 WHILE 迴圈,直到滿足結束條件。簡單的例子:
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;
將執行此迴圈,直到變數 v_counter 的當前值小於 10。
結果:
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
最重要的是,我們的迴圈以'0’值開頭,因此第一行結果是’當前迭代迴圈為 0’。