我想基于动态填充的参数在Oracle中运行FOR LOOP。请参阅代码的外观:
declare
idx number := (select max(field_name) from table_name);
begin
for i in 1..idx loop
dbms_output.put_line (i);
end loop;
end;
所以基本上如果select max(field_name) from table_name
返回例如10,那么循环应该运行10次(for i in 1..10 loop
)。
答案 0 :(得分:2)
假设field_name
是一个整数,您只需要以正确的方式获取最大值:
declare
idx number;
begin
select max(field_name)
into idx
from table_name;
if idx is not null then -- to handle the case where the table is empty
for i in 1..idx loop
dbms_output.put_line (i);
end loop;
end if;
end;
或没有IF
:
declare
idx number;
begin
select nvl(max(field_name), 0)
into idx
from table_name;
for i in 1..idx loop
dbms_output.put_line (i);
end loop;
end;