有两种PL / SQL FOR循环:数字FOR循环和游标FOR循环。
数字FOR循环是传统的。当循环开始时,FOR循环的迭代次数是已知的。
这里是数字FOR循环的一般语法:
FOR loop index IN [REVERSE] lowest number .. highest number
LOOP
executable statement(s)
END LOOP;
您必须在LOOP和END LOOP关键字之间至少有一个可执行语句。
实例
下面的循环执行10次; loop_counter从1开始,结束于10:
FOR loop_counter IN 1 .. 10
LOOP
--executable statements ...
END LOOP;
下面的循环执行10次; loop_counter从10开始,结束于1:
FOR loop_counter IN REVERSE 1 .. 10
LOOP
---executable statements ...
END LOOP;
这里是一个循环,甚至不执行一次。
FOR loop_counter IN REVERSE 10 .. 1
LOOP
---executable statements ...
END LOOP;
以下循环对由变量和表达式中的值确定的范围执行:
FOR calc_index IN start_period_number ..
LEAST (end_period_number, current_period)
LOOP
--executable statements ...
END LOOP;
实例2
循环范围的边界可以是文字,变量或表达式,但它们必须求值为数字。
DECLARE
first INTEGER := 1;
last INTEGER := 10;
high INTEGER := 100;
low INTEGER := 12;
BEGIN
-- Bounds are numeric literals:
FOR j IN -5..5 LOOP
NULL;
END LOOP;
-- Bounds are numeric variables:
FOR k IN REVERSE first..last LOOP
NULL;
END LOOP;
FOR step IN 0..(TRUNC(high/low) * 2) LOOP
NULL;
END LOOP;
END;
/
实例3
在FOR-LOOP语句中使用计数器的递增
DECLARE
TYPE DateList IS TABLE OF DATE INDEX BY PLS_INTEGER;
dates DateList;
BEGIN
FOR j IN 1..3 LOOP
dates(j*5) := SYSDATE;
END LOOP;
END;
/
循环边界的动态范围
PL / SQL允许您通过使用变量的边界在运行时指定循环范围。
CREATE TABLE temp (
emp_no NUMBER,
email_addr VARCHAR2(50)
); -- www .ja v a 2 s . com
DECLARE
emp_count NUMBER := 4;
BEGIN
FOR i IN 1..emp_count LOOP
INSERT INTO temp
VALUES(i, 'to be added later');
END LOOP;
END;
/
select * from temp;
drop table temp;
上面的代码生成以下结果:
注意
如果循环范围的下限大于上限,则不执行循环体。
使用现有变量作为循环变量
DECLARE
i NUMBER := 5;
BEGIN
FOR i IN 1..3 LOOP
DBMS_OUTPUT.PUT_LINE ('Inside loop, i is ' || TO_CHAR(i));
END LOOP;
DBMS_OUTPUT.PUT_LINE ('Outside loop, i is ' || TO_CHAR(i));
END;
/
上面的代码生成以下结果:
引用与内部计数器名称相同的外部计数器
BEGIN
<>
FOR i IN 1..3 LOOP
<>
FOR i IN 1..3 LOOP
IF outer_loop.i = 2 THEN
DBMS_OUTPUT.PUT_LINE
( 'outer: ' || TO_CHAR(outer_loop.i) || ' inner: '
|| TO_CHAR(inner_loop.i));
END IF;
END LOOP inner_loop;
END LOOP outer_loop;
END;
/
上面的代码生成以下结果:
EXIT的示例
您可以使用标签给循环命名。PL / SQL中的循环标签具有以下格式:
<>
其中label_name是标签的名称,并且该循环标签紧接在LOOP语句之前出现:
<>
FOR emp_rec IN emp_cur
LOOP
...
END LOOP;
EXIT语句使FOR循环提前完成。
DECLARE
v_employees employees%ROWTYPE;
CURSOR c1 is SELECT * FROM employees;
BEGIN
OPEN c1;
-- Fetch entire row into v_employees record:
FOR i IN 1..10 LOOP
FETCH c1 INTO v_employees;
EXIT WHEN c1%NOTFOUND;
-- Process data here
END LOOP;
CLOSE c1;
END;
/
EXIT与FOR LOOP中的标签
DECLARE
v_employees employees%ROWTYPE;
CURSOR c1 is SELECT * FROM employees;
BEGIN
OPEN c1;
<>
FOR i IN 1..10 LOOP
-- Process data here
FOR j IN 1..10 LOOP
FETCH c1 INTO v_employees;
EXIT outer_loop WHEN c1%NOTFOUND;
-- Process data here
END LOOP;
END LOOP outer_loop;
CLOSE c1;
END;
/
游标FOR循环
游标FOR循环是与显式游标相关联的循环。
下面是游标FOR循环的基本语法:
FOR record IN { cursor_name | (explicit SELECT statement) }
LOOP
executable statement(s)
END LOOP;
其中记录是由PL / SQL使用%ROWTYPE属性对由cursor_name指定的游标隐式声明的记录。
循环的游标示例
DECLARE
CURSOR c_my IS SELECT id, room_number FROM occupancy;
my_rec c_my%ROWTYPE;
BEGIN
OPEN c_my;
LOOP
FETCH c_my INTO my_rec;
EXIT WHEN c_my%NOTFOUND;
update_bill(my_rec.id, my_rec.room_number);
END LOOP;
CLOSE c_my;
END;