PL/SQL 基本案例

263 阅读2分钟

PL/SQL 基本案例

案例1

问题: 1.统计每年入职的员工人数

解题思路:

image-20210620170512745

循环遍历员工人数,取出每一个员工的入职日期,符合的,计数器加1

代码:

set severoutput on

declare 
  -- 定义光标
  cursor cemp is select to_char(hiredate,'yyyy') from emp;
  phiredate varchar2(4);
  
  --每年入职的员工数
  count80 number :=0;
  count81 number :=0;
  count82 number :=0;
  count87 numner :=0;
  
  begin
  -- 打开光标
  open cemp;
  
  loop
  --取出一个员工的入职年份
  fetch cemp into phiredate;
  exit when cemp%notfund;
  
  --判断入职年份
  if phiredate = '1980' then count80 :=count80+1;
  elsif phiredate ='1981' then count81 :=count81+1;
  elseif phiredate ='1982' then count82 :=count82+1;
  else count87 := count87+1;
  
  end loop;
  
  --关闭光标
  close cemp;
  end;
  /

案例2

问题:2 为员工涨工资。从最低工资涨起每人长10%,但工资总额不能超过5万元,请计算涨工资的人数和涨工资后的工资总额,并输出涨工资的人数及工资总额。

解题思路:

image-20210620173022428

代码:

set serveroutput on

declare
-- 定义光标
   cursor cemp is select empno,sal from emp order by sal;
   pempno emp.empno%type;
   psal emp.sal%type;
 
 --涨工资的人数
  countEmp number := 0;
  
  --涨后的工资总额
  salTotal number;
begin
   --得到工资的总额的初始化值
   select sum(sal) into salTotal from emp;
   
   --打开光标
   open cemp;

    loop
    --1,工资总额大于 5W
    exit when salTotal > 5000;
    
    --取一个员工涨工资
    fetch cemp into pempno ,psal;
    exit when cemp%notfund;
    
    --涨工资
    update  emp set sal=sal*1.1 when empno=pempno;
    --人数+1;
    countEmp := countEmp+1;
    
    --涨后的工资总额 = 涨前的工资总额 + sal * 0.1
    salTotal :=salTotal + psal * 0.1;
    end loop;
   
   --关闭光标
   close cemp;
   
   commit;
end;
/

案例3

问题

image-20210620180333139

解题思路

image-20210620180724606

代码

set serveroutput on;
declare 

  -- 定义系光标
  cursor cdept is select dno,dname from dep;
  pdno dep.dno%type;
  pdname dep.dname%type;
  
  --定义成绩的光标
  cursor  cgrade(coursename varchar2,depno number)
      is select grade from sc where cno=(select cno from course where cname = coursename)
                                    and sno = (select sno from student where dno = depno);
                                    
  pgrade sc.grade%type;
  --每个分数段的人数
  count1 number,count2 number,count3 number;
  
  --每个系选择 “大学物理系学生的平均成绩”
  avggrade number;
  
  --课程名称
  pcourseName varchar2 := "大学物理";
begin 
    --打开系的光标
    open cdept;
    loop 
    --取一个系的信息
    fetch cdept into pdno,pdname;
    exit when cdept%notfund;
    
    --初始化工作
    count1 := 0; count2 :=0; count3 :=0;
    
    --系的平均成绩
    select avg(grade) into avggrade from sc where cno =(select cno from course where cname=pcoursename)
                      and sno in (select sno from student where dno =pdno )
     
     -- 取系中,选修了大学物理系的学生成绩
     open cgrade(pcourseName,pdno);
     
     loop
       --取一个学生的成绩
       fetch cgrade  into pgrade;
       exit when cgrade%notfound;
       
       --判断成绩的范围
       if pgrade < 60 then count1 := count1+1;
       elseif pgrade >=60 and pgrade <=85 then count2 :=count2+1;
       else count3 := count3 + 1;
       end if;
       
       end loop;
       close cgrade;
       
       --保存当前结构
       insert into msg1 values(pcourseName,pdno,count1,count2,count3,avggrade);
       
       
    end loop;
end;
/
  

有不对的地方,欢迎大家一起讨论。 最后,欢迎大家关注我的微信号“涛涛之海”,您的点赞,收藏,转发就是对我的最大鼓励。