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;
/
转发至微博
转发至微博
--定义两个游标保存结果
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;
/
转发至微博
转发至微博
相关文章推荐
- Oracle连接查询
- mysql优化技术
- oracle数据库之sql查询语句笔记
- oracle数据库笔记之数据库基本管理
- 使用sqoop将oracle数据迁移到hive中
- jdbc数据库连接信息
- 将SQL Server数据库转成SQL语句
- 小谈c#数据库存取图片的方式
- ubuntu mysql 配置文件my.cnf 更改tmp路径
- where 与 having
- Oracle db中 CONNECT role的含义
- 查询最近7天的每天的注册人数
- 关于Redis的知识汇总[转]
- mysql组合索引与字段顺序
- 如何替换orcl实例下的四个数据库
- oracle字段xml特殊字符处理
- mysql日志文件
- sql查询时加上行号
- oracle TRANSLATE函数详解
- mysql无法导入sql语句