首页 > 代码库 > PLSQL编程基础 :分支、循环语句

PLSQL编程基础 :分支、循环语句

  一、分支语句

set serveroutput on
declare
num number;
begin
  num:=3;
  if num = 5
    then
      dbms_output.put_line(这个数字是5);
    elsif num >7
    then 
      dbms_output.put_line(这个数字大于7);
    else
      dbms_output.put_line(其他数字);
  end if;
end;

  二、循环语句

    1.loop循环

set serveroutput on
declare
num number;
begin
  num := 1;
    loop
       exit when num > 10;
       dbms_output.put_line(num);
       num := num + 1;
     end loop;
end;

    2.while循环

set serveroutput on
declare
num number;
begin
  num := 1;
 while num < 10 
    loop
      dbms_output.put_line(num);
      num := num + 1;
    end loop;
end;

    3.for循环

set serveroutput on
declare
num number;
begin
  num := 1;
    for ii in (select sname from student)
    loop
      dbms_output.put_line(ii.sname);
    end loop;
end;

 

PLSQL编程基础 :分支、循环语句