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’。