方法一: 百度找一找
方法二:看之前的视频自己手动输入
方法三:重装数据库
难易程度由上到下
declare
cursor cdept is select deptno from dept;
pdeptno dept.deptno%type;
cursor cemp(dno number) is select sal from emp where deptno = dno;
psal emp.sal%type;
count1 int;
count2 int;
count3 int;
totalMon number := 0;
flag number;
begin
open cdept;
--外层循环
loop
fetch cdept into pdeptno;
exit when cdept%notfound;
--判断部门是否存在,如果部门不存在直接退出所有循环
select count(1) into flag from emp where deptno = pdeptno;
if flag = 0 then return;
end if;
--第一层循环内给变量赋值为0,保证每次内层循环的计数器都从零开始(必须要写外层循环内,内层循环外)
count1 := 0;
count2 := 0;
count3 := 0;
open cemp(pdeptno);
loop
select sum(sal) into totalMon from emp where deptno = pdeptno;
fetch cemp into psal;
exit when cemp%notfound;
if psal <3000 then count1 := count1+1;
elsif psal <6000 then count2 := count2+1;
else count3 := count3+1;
end if;
end loop;
close cemp;
--保存到msg表
insert into msg values(pdeptno,count1,count2,count3,totalMon);
--输出
--dbms_output.put_line('部门:'||pdeptno||' 3000以下为:'||count1||' 3000-6000为:'||count2||' 6000以上为:'||count3||' 总额为:'||totalMon);
end loop;
close cdept;
commit;
dbms_output.put_line('统计完成');
end;
/
你所查询的表中没有符合条件的,所以没有选定行。
group by后面要加变量名,会造成每个值的数量统计,而不能按照变量值区间分段统计
是的。
cemp%notfound; 判断的是 cemp 指针移动后的字段,还是移动前的字段
这样是可以的,第二个loop没有执行,你看一下你的光标的中是否有值,很大可能值判断notfound直接退出了
那是从部门表dept查的,不是从员工表emp查的,部门表的部门名称和部门编号都是唯一的,你可以查一下:select * from dept;
第二个loop循环中,结束条件是exit when cemp%notfound,
saltotal := saltotal+psal;这一段计算出来的不应该是所有部门员工工资总和么
光标退出的条件,在循环开始取数后,根据是否取到数据来判断退出。
oracle 不区分大小写,但是每个人有每个人的书写习惯,你也可以都大写或者都小写,自己觉得好就行