ORACLE 循环

1、 Exit When 循环:

 

declare 
  -- Local variables here
  i integer;
begin
  i:=0;
  LOOP
  Exit When(i>5);
       Dbms_Output.put_line(i);
       i:=i+1;
  END LOOP;
end;
 

 

2、 Loop 循环

 

declare 
  -- Local variables here
  i integer;
begin
  i:=0;
  loop
    i:=i+1;
    dbms_output.put_line(i);
    if i>5 then
       exit;
    end if;
  end loop;
end;
 

3、 While 循环:

 

declare 
  -- Local variables here
  i integer;
begin
  i:=0;
  while i<5 loop
     i:=i+1;
     dbms_output.put_line(i);
  end loop;
end;
 

4、 For 普通循环:

 

declare 
  -- Local variables here
  i integer;
begin
  i:=0;
  for i in 1..5 loop
      dbms_output.put_line(i);
  end loop;
end;
 

5 For 游标循环:

    准备数据

 

--创建表
create table test (id number);

--插入数据
declare 
  -- Local variables here
  i integer;
begin
  i:=0;
  for i in 1..5 loop
      insert into test values(i);
  end loop;
end;

    循环

declare 
  -- Local variables here
  begin
    for c_test in (select * from test) loop
           dbms_output.put_line(c_test.id);
  end loop;
  
end;

 

猜你喜欢

转载自zhuyuehua.iteye.com/blog/1697374