oracle学习笔记5:pl/sql流程控制语句

pl/sql编程中的流程控制语句主要有if语句,case语句以及循环语句

下面将逐个介绍这几个语句

1.if语句

-- 根据员工工资来判断奖金的多少,工资在5000以上的奖金500,工资在3000-5000的奖金300,其他的工资100;
declare
  v_emp emp%rowtype;
  begin
  select * into v_emp from emp where empno=7369;
  if v_emp.sal>=5000 then
    update emp set comm=nvl(comm,0)+500  where empno=v_emp.empno ;
  elsif v_emp.sal<5000 and v_emp.sal>=3000 then
    update emp set comm=nvl(comm,0)+300 where empno=v_emp.empno ;
  else
    update emp set comm=nvl(comm,0)+100  where empno=v_emp.empno ;
  end if;
end;

2.case语句

-- 根据部门的编号判断部门是哪儿里
select deptno,
       case deptno
         when 10 then '研发部'
         when 20 then '市场部'
         when 30 then '人事部'
         end 部门
from dept;

3.循环语句

3.1 loop循环

-- loop循环是最简单的循环
declare
  i number:=1;
  begin
  loop
--     执行循环体
    dbms_output.put_line('hello'||i);
--     退出循环的条件
    exit when i>=5;
    i:=i+1;
  end loop;
end;

3.2 while循环

declare
  i number(2):=1;
  begin
--   while循环,先执行条件,如果条件满足,就执行循环体,否则就不执行
    while i<=5 loop
      dbms_output.put_line('hello'||i);
--       i++;
      i:=i+1;
    end loop;
end;

3.3 for循环

declare
  begin
--   相当于java中的for循环
--   for 递增的变量 in 下限..上限 loop
--       循环体
--   end loop;
    for i in 1..10 loop
      dbms_output.put_line('hello'||i);
  end loop;
end;

3.4 标号和GOTO

-- GOTO label
--   <<label>>
declare
  begin
--   当i=5时,跳出循环
  for i in 1..10 loop
    dbms_output.put_line('hello'||i);
    if i=5 then
      GOTO label;
    end if;
  end loop;
  <<label>>
  dbms_output.put_line('world');
end;

猜你喜欢

转载自www.cnblogs.com/Zs-book1/p/11228021.html