Oracle PL/SQL,实例

实例一:统计各年份的入职员工数

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

实例二:涨工资


--涨工资。每人涨10%,但所有员工工资总额不超过50000。计算涨工资的人数,涨后的工资总额。

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 > 50000;
         --取一个员工
         fetch cemp into pempno,psal;
         --2. notfound
         exit when cemp%notfound;
         
         --涨工资
         update emp set sal=sal*1.1 where empno=pempno;
         --人数+1
         countEmp := countEmp +1;
         --涨后=涨前 + sal *0.1
         salTotal := salTotal + psal * 0.1;

    end loop;
    close cemp;  --关闭光标
    
    commit;  --有更新数据表的操作,需要提交事务
    dbms_output.put_line('人数:'||countEmp||'   总额:'||salTotal);
end;
/

实例三:根据工资范围统计每个部门员工数(嵌套循环(游标))

--根据工资范围统计每个部门员工数,并计算各部门工资总额

declare
  --部门
  cursor cdept is select deptno from dept;  --外层循环(游标)
  pdeptno dept.deptno%type;
  
  --部门中员工的薪水
  cursor cemp(dno number) is select sal from emp where deptno=dno;  --内层循环(游标),带参数的游标,dno是形参
  psal emp.sal%type;
  
  --每个段的人数
  count1 number; count2 number; count3 number;
  --部门的工资总额
  salTotal number := 0;
begin
  --部门
  open cdept;
  loop  --外层(游标)循环,循环所有的部门
       --取一个部门
       fetch cdept into pdeptno;
       exit when cdept%notfound;
       
       --初始化
       count1:=0; count2:=0; count3:=0;
       --得到部门的工资总额
       select sum(sal) into salTotal from emp where deptno=pdeptno;
       
       --取部门的中员工薪水
       open cemp(pdeptno);
       loop  --内层(游标)循环,循环部门所有的员工
            --取一个员工的薪水
            fetch cemp into psal;
            exit when cemp%notfound;
            
            --判断
            if psal < 3000 then count1:=count1+1;
               elsif psal >=3000 and psal<6000 then count2:=count2+1;
               else count3:=count3+1;
            end if;
       end loop;
       close cemp;

       --保存结果
       insert into msg values(pdeptno,count1,count2,count3,nvl(saltotal,0));

  end loop;
  close cdept;
  
  commit;
  dbms_output.put_line('完成');
  
end;
/

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82421909