制御構造のスクラップ

while文。


set serveroutput on;

declare
cnt number;
now date;
begin
cnt := 0;
while cnt < 10 loop
select sysdate into now from dual;
DBMS_OUTPUT.PUT_LINE(cnt || ': ' || now);
cnt := cnt + 1;
end loop;
end;
/


0: 2005-01-20 12:47:48
1: 2005-01-20 12:47:48
2: 2005-01-20 12:47:48
3: 2005-01-20 12:47:48
4: 2005-01-20 12:47:48
5: 2005-01-20 12:47:48
6: 2005-01-20 12:47:48
7: 2005-01-20 12:47:48
8: 2005-01-20 12:47:48
9: 2005-01-20 12:47:48

if文。


set serveroutput on;

declare
cnt number;
begin
for cnt in 1..5 loop
if cnt < 2 then
DBMS_OUTPUT.PUT_LINE(cnt || 'は2未満です');
elsif cnt < 4 then
DBMS_OUTPUT.PUT_LINE(cnt || 'は2以上4未満です');
else
DBMS_OUTPUT.PUT_LINE(cnt || 'は4以上です');
end if;
end loop;
end;
/


1は2未満です
2は2以上4未満です
3は2以上4未満です
4は4以上です
5は4以上です

loop文。


set serveroutput on;

declare
cnt number;
begin
cnt := 0;
loop
DBMS_OUTPUT.PUT_LINE(cnt);
cnt := cnt + 1;
exit when cnt >= 5;
end loop;
end;
/


0
1
2
3
4