PLSql--cursor 显式游标(参数)

本文提供了两个使用PLSQL进行批量处理的实际案例。第一个案例展示了如何统计各部门的工资分布及总额,第二个案例则介绍了如何统计特定课程的学生成绩分布及平均成绩,并将结果存入数据库。

此为以前写的代码,整理中,未完待续。

此文注重思维逻辑
在写PLsql时应先按如下方式或其他方式捋清楚了再写,这样效率更高。

问题1

各部门工资情况分布和总额

分析

SQL语句

涉及表范围dept(dno);emp(dno,sal);
1,得到部门号
select dno from dept;
–> cursor –> loop –> exit when unfound
2,该部门人员的工资
slect sal from emp where dno =??
–> cursor(deptname) –> loop –>exit when unfound

变量:1,哪些变量 2,如何得到值
–>各工资段个数
count1 number;count2 number;count3 number;
–>总额
sumsal number;

如下有两种计算总和方式
(1)sum:=slect sum(sal) from emp where dno =??
(2)累加

解决

set serveroutput on

declare
cursor d is select dno from dept;
deptno dept.dno%type;
cursor s(pdno number) slect sal from emp where dno=pdno;
sals empp.sal%type;
count1 number;count2 number;count3 number;
sumsal number;
begin
open d;
loop
fetch d into deptno;
exit when d%unfound;
count1:=0;
count2:=0;
count3:=0;
select sum(sal) into sumsal from emp where dno =deptno;
open s(deptno);
loop 
fetch s into sals;
exit when s%unfound;

if sals >6000 then count1:=count+1;
else if sals <=6000 and sals >3000 then count2:=count2+1;
else count3:=count3+1;
end if;

end loop;
close s;

end loop;
close d;
dbms_output.putline('完成');

exception 
when no_data_found then dbms_output.put_line('..');
end;

说明

cursor 游标打开后一定要关闭,格式如下:
open ex1[(par1[,par2,…])] loop fetch ex1 into … exit when ex1%… … end loop; close ex1;

问题2

统计该系选修了大学物理课所有学生的成绩分布情况和平均成绩,并存储到数据库中

分析

SQL语句

涉及表范围dept(dno,dname);S(sno,dno);SC(sno,cno,grade);C(cno,cname);
1,得到系名
select dno,dname from dept;
–> cursor -> loop -> exit when unfound(异常情况)
2,在该系中,选修‘大学物理’该生的成绩情况
select grade from SC,C where SC.cno=C.no and C.cname=’大学物理’ and SC.sno IN (select sno from S where dno==?? )
–> cursor(depno) -> loop -> exit when unfound

变量:1,有哪些变量;2,变量值如何得到
–>系中选修该课的不同成绩个数
count1 number; count2 number; count3 number;
avggrade number;

如下有两种求平均成绩方式
(1) countgrade number; sumgrage number;avggrade:=sumgrage/countgrade;
(2) select avg(grade) into avggrade from SC,C where SC.cno=C.no and C.cname=’大学物理’ and SC.dno IN (select sno from dept where dno=?? )

解决

set serveroutput on

declare
cursor d is select dno,dname from dept;
pdno dept.dno%type;
pdname dept.dname%type;

cursor g(coursename varchar2;deptno number) is select grade from SC,C where SC.cno=C.cno and C.cname=coursename and S.sno IN (select sno from dept where dno=deptno );

pgrade sc.grade%type;
count1 number; 
count2 number; 
count3 number;
avggrade number;
countgrade number; 
sumgrage number;
pcname varchar2:='大学物理';
begin
open d;
loop 
fetch d into pdno,pdname;
exit when d%unfound;
count1:=0;
count2:=0;
count3:=0;
avggrade:=0;
countgrade:=0;
sumgrage:=0;
open g(pcname,pdno);
loop
fetch g into pgrade;
exit when g%unfound;
-->判断成绩
if grade <60 then count1:=count1+1;
else if grade>=60 and grade<=85 then count2:=count2+1;
else count3:=count3+1;
end if;
-->统计该系选修了该课所有学生的个数与总成绩
countgrade:=countgrade+1;
sumgrage:=sumgrage+grade;
end loop;

close g;
-->统计该系选修了该课所有学生的平均成绩
avggrade:=sumgrage/countgrade;
-->select avg(grade) into avggrade from SC,C where SC.cno=C.cno and C.cname=coursename and S.sno IN (select sno from dept where dno=deptno );
end loop;
close d;

insert into result value(pcname,pdname,count1,count2,count3,avggrade);
commit;
dbms_output.put_line('完成');

exception
when zero_divide then dbms_output.put_line('被除数为零');
when no_data_found then dbms_output.put_line('..');
when others then dbms_output.put_line('其他');

end;

说明

如果表result不存在,则需要事先创建好

补充exception

  1. when no_data_found then dbms_output.putline(‘bad’);
  2. when too_many_rows then dbms_output.putline(‘bad’);
  3. when zero_divide then dbms_output.putline(‘bad’);
  4. when value_error then dbms_output.putline(‘bad’);
  5. when timeout_on_resource then dbms_output.putline(‘bad’);
  6. when others then dbms_output.putline(‘other’);
  7. 自定义
短剧源码/短剧系统/短剧系统源码【附短剧系统搭建教程】效果截图https://yuanmawww.seoheimao.cn/437.html2026最新短剧系统源码 支持广告会员功能齐全短剧源码支付对接的是易支付,官方支付短剧视频上传支持上传到本地、oss或者填写外链地址,支持采集苹果 CMSv10 热门短剧模板是一款专为短剧内容打造的高效模板。它采用了的设计理念和技术,为您的短剧网站提供了一个时尚、现代且用户友好的界面。这个模板具有以下特点:热门短剧展示:精心设计的布局,突出展示热门短剧,吸引观众的注意力。简洁美观:简洁的设计风格,注重内容展示,提供舒适的视觉体验。高度自定义:可根据您的需求轻松自定义模板颜色、字体、布局等,打造独特的品牌形象。响应设计:适应各种设备,确保在桌面、平板和手机上都能完美展示。优化性能:经过优化,确保网站加载速度快,提升用户体验。易于使用:无需复杂的编程知识,简单的后台管理界面,方便内容更新和维护。无论您是短剧制作公司、内容创作者还是站长,这个模板都将帮助您快速搭建一个专业、吸引人的短剧网站,吸引更多观众,提升您的内容影响力。短剧功能包含:1.支持会员模,支持用户单独购买等等多功能;2.付费观看(强大的支付系统,支持多平台支付方,支付灵活可配置,多重加密确保交易安全);3.成熟代理机制(主流代理机制让流量不在是问题,配合多营销方,为推广主力切实解决流量;4.优化前端ui,界面更美观炫酷;短剧源码/短剧系统/短剧系统源码【附短剧系统搭建教程】
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值