ORACLE的循环

ORACLE的循环

LOOP循环

declare
num number;
begin 
  num:=1;
loop
  dbms_output.put_line('yes');
  IF num >= 10 THEN
      EXIT;
    END IF;
    num := num + 1;
end loop;
end;

WHILE 循环

declare
num stu_info.id%type;
begin
	num:=0;
	while num<10 
	loop
		dbms_output.put_line(num);
		 num:=num+1;
	end loop;
end;

FOR 循环

declare
begin 
  for i in 1..10
  loop
    dbms_output.put_line(i);
  end loop;
end;
DECLARE
  -- 定义循环的起始值、结束值和步长
  start_num NUMBER := 1;
  end_num NUMBER := 10;
  step NUMBER := 2;
BEGIN
  FOR i IN start_num..end_num LOOP
    DBMS_OUTPUT.PUT_LINE(i);
    -- 在循环中添加步长
    i := i + step;
  END LOOP;
END;

带SQL语句的FOR循环

declare 
s_id stu_info.id%type;
begin 
  for i in 1..100
    loop
      begin
        select id into s_id from stu_info where id=2;
        dbms_output.put_line(s_id);
      end;
    end loop;
end;

你可能感兴趣的:(oracle,数据库)