您的位置:首页 > 数据库

plsql

2015-12-24 10:29 525 查看
declare
 
 --定义两个游标保存结果
   cursor c1 is select
distinct deptno from dept;
   cursor c2(pdno number) is
select sal from emp where deptno=pdno;
   
 
 --定义三个变量用于保存每个部门三个工资段的人数
   count1 NUMBER;
   count2 number;
   count3 number;
   
 
 --记录c1游标中的部门号
   pdeptno dept.deptno%
TYPE;
 
 --记录c2游标中的薪水值
   psal emp.sal% TYPE;
begin
  open c1;--打开c1
获得所有部门号
    loop
      fetch
c1 into pdeptno;--取一个部门号
      exit
when c1%notfound;
     
--计数器清零    
 
     
count1 := 0; 
     
count2 := 0;
     
count3 := 0;
     
--得到该部门的所有员工
      open
c2(pdeptno);
     
  loop
     
    fetch c2 into psal;
--得到该员工的工资
     
    exit when c2%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
c2;
   
  
     
--保存该部门的统计结果
     
insert into salcount values(pdeptno,count1,count2,count3);
     
commit;
    end loop;
  close c1;
end;
/
   
   
   
   
   
   
   
   
   
   
   
   



转发至微博
 



转发至微博
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: