1.簡(jiǎn)單循環(huán)
loop
sequence of statements;
end loop;
例:
declare
a number:=100;
begin
loop
a:=a+25;
exit when a=250;
end loop;
dbms_output.put_line(to_char(a));--注意這里用to_char()
end;
2.For循環(huán)
For counter in [Reverse]lowerbound…upperbound
loop
sequence of statements;
end loop;
counter是計(jì)數(shù)器變量,此處不用聲明,此變量在循環(huán)體中只能讀取,不能賦值。如i++是錯(cuò)誤的
lowbound是下界
upperbound是上界,默認(rèn)情況下,從下界到上界進(jìn)行迭代,如果使用關(guān)鍵字Reverse,則從上界到下界進(jìn)行迭代
例:
begin
for i in 1..2--如果反向,使用Reverse關(guān)鍵字即可
loop
update order_master set ostatus='p' where odate<sysdate;
end loop;
end;
3.While循環(huán)
while <condition>
loop
sequence of statements;
end loop;
例:
declare
i number:=0;
j number:=0;
begin
while i<=100 loop
j:=j+i;
i:=i+2;
end loop;
end;