課程
/數據庫
/Oracle
/Oracle數據庫開發必備利器之PL/SQL基礎
為什么我insert之后就直接掛起了,需要手動commit,后面的語句都不執行了
2019-07-21
源自:Oracle數據庫開發必備利器之PL/SQL基礎 5-4
正在回答
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;
/
舉報
Oracle數據庫高級開發必備的基礎,通過實例帶你熟練掌握
2 回答為什么我連接不了ORACLE?
2 回答為什么在end之后要加一個斜杠“/”啊
1 回答為什么每次我在developer上一運行腳本就死掉?
1 回答老師我想問一下,為啥psql新建連接是一直出錯呀
2 回答最后為什么要測試?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2019-07-21
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;
/