对于Oracle PL / SQL中的In循环

时间:2016-04-12 15:54:53

标签: sql oracle for-loop plsql for-in-loop

在PL / SQL中,我可以使用FOR IN循环从1-20开始迭代:

FOR counter IN 1..20

我想知道的是,我可以做一个简单的改动,这将允许我只在1-20之间的偶数值迭代循环吗?即计数器将进入2,4,6,...... 20

2 个答案:

答案 0 :(得分:2)

加入戈登的评论......

这是语法所以没有内置方式

http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/controlstatements.htm#LNPLS411

[ label ] FOR index IN [ REVERSE ] lower_bound..upper_bound LOOP
  statements
END LOOP [ label ];

您可以在FOR LOOP语句中模拟STEP子句

http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/controlstatements.htm#BCGJHACI

DECLARE
  step  PLS_INTEGER := 5;
BEGIN
  FOR i IN 1..3 LOOP
    DBMS_OUTPUT.PUT_LINE (i*step);
  END LOOP;
END;

答案 1 :(得分:0)

SET SERVEROUTPUT ON;--default buffer size is 1000 000
declare 
idx number:=0;
step number:=10000;
begin
DBMS_OUTPUT.ENABLE(Null);-- unlimited output buffer
for i in 1..3 loop
 DBMS_OUTPUT.PUT_LINE('idex: from [' ||idx|| ' ] to [' || (idx+step) || ']');
 --..... you loop logic
  select t.id, row_number() over(order by t.id) rw
         from t
         where rw >= idx and rw < (idx+step)
 idx:= idx+step; --increment step.
end loop;
end;