PLSql--cursor 游标(参数)

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

此文注重思维逻辑
在写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. 自定义

你可能感兴趣的:(数据库,plsql,Cursor,思维逻辑)