我想使用这个简单的循环:
FOR indx IN 1 .. l_table_with_227_rows.COUNT
但不是使用1..row.count,而是要指定数组或列的下限和上限。 示例:employee_ID [1] .. employee_ID [10] 请记住,我想通过滑动重复循环来更改它们 接下来会的 employee_ID [11] .. employee_ID [20]
你知道这是否可能? 我一直在寻找答案
谢谢,
答案 0 :(得分:0)
是的,您可以使用数组的值来指定FOR
循环的下限和上限值。
例如,假设我们有一个员工表,其数据如下:
create table EMPLOYEES (id number, name varchar2(20), title varchar2(30), salary number);
insert into EMPLOYEES values (1, 'Jim', 'App Specialist', 1000);
insert into EMPLOYEES values (2, 'Joseph', 'App Specialist', 2000);
insert into EMPLOYEES values (3, 'John', 'App Specialist', 3000);
insert into EMPLOYEES values (4, 'James', 'App Specialist', 4000);
insert into EMPLOYEES values (5, 'Jack', 'App Specialist', 5000);
insert into EMPLOYEES values (6, 'Jim', 'App Specialist', 1000);
insert into EMPLOYEES values (7, 'Joseph', 'App Specialist', 2000);
insert into EMPLOYEES values (8, 'John', 'App Specialist', 3000);
insert into EMPLOYEES values (9, 'James', 'App Specialist', 4000);
insert into EMPLOYEES values (10, 'Jack', 'App Specialist', 5000);
insert into EMPLOYEES values (11, 'Jim', 'App Specialist', 1000);
insert into EMPLOYEES values (12, 'Joseph', 'App Specialist', 2000);
insert into EMPLOYEES values (13, 'John', 'App Specialist', 3000);
insert into EMPLOYEES values (14, 'James', 'App Specialist', 4000);
insert into EMPLOYEES values (15, 'Jack', 'App Specialist', 5000);
insert into EMPLOYEES values (16, 'Jim', 'App Specialist', 1000);
insert into EMPLOYEES values (17, 'Joseph', 'App Specialist', 2000);
insert into EMPLOYEES values (18, 'John', 'App Specialist', 3000);
insert into EMPLOYEES values (19, 'James', 'App Specialist', 4000);
insert into EMPLOYEES values (20, 'Jack', 'App Specialist', 5000);
insert into EMPLOYEES values (21, 'Jim', 'App Specialist', 1000);
insert into EMPLOYEES values (22, 'Joseph', 'App Specialist', 2000);
insert into EMPLOYEES values (23, 'John', 'App Specialist', 3000);
insert into EMPLOYEES values (24, 'James', 'App Specialist', 4000);
insert into EMPLOYEES values (25, 'Jack', 'App Specialist', 5000);
commit;
以下过程包括FOR
循环,它使用数组中的值(id_list)来指定绑定值。
create or replace procedure PRINT_EMPLOYEES
as
cursor C_EMPLOYEES is
select id, name, title, salary from EMPLOYEES ORDER BY id;
type C_LIST is TABLE OF EMPLOYEES.id%type INDEX BY pls_integer;
id_list C_LIST;
COUNTER integer := 0;
begin
DBMS_OUTPUT.PUT_LINE(sysdate);
--Fill the array using values from cursor
for EMP_REC in C_EMPLOYEES
LOOP
ID_LIST(COUNTER) := EMP_REC.id;
COUNTER := COUNTER + 1;
end loop;
--Outer loop goes from 1 to 3
for OUTER_COUNTER in 1..3
LOOP
DBMS_OUTPUT.PUT_LINE(ID_LIST(OUTER_COUNTER) || ' ~ ' || ID_LIST(OUTER_COUNTER+5));
--Inner loop runs from id_list (outer_counter) to id_list(outer_counter + 5)
for counter in id_list(OUTER_COUNTER)..id_list(OUTER_COUNTER+5)
LOOP
DBMS_OUTPUT.PUT_LINE(ID_LIST(COUNTER));
end LOOP;
END LOOP;
end;
<强>参考强>:
Associative Arrays on Oracle® Database PL/SQL Language Reference
Working with Collections on Oracle Magazine Code Listing 2: Display all strings in a collection
使用FOR循环中数组的第一个索引值和最后一个索引值(此示例显示了定义FOR循环限制的另一种方法)。