課程
/數(shù)據(jù)庫(kù)
/Oracle
/Oracle數(shù)據(jù)庫(kù)開(kāi)發(fā)必備利器之PL/SQL基礎(chǔ)
為什么我insert之后就直接掛起了,需要手動(dòng)commit,后面的語(yǔ)句都不執(zhí)行了
2019-07-21
源自:Oracle數(shù)據(jù)庫(kù)開(kāi)發(fā)必備利器之PL/SQL基礎(chǔ) 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;
? --外層循環(huán)
? loop
? ? fetch cdept into pdeptno;
? ? exit when cdept%notfound;
? ? --判斷部門是否存在,如果部門不存在直接退出所有循環(huán)
? ? select count(1) into flag from emp where deptno = pdeptno;
? ? if flag = 0 then return;
? ? end if;
? ? --第一層循環(huán)內(nèi)給變量賦值為0,保證每次內(nèi)層循環(huán)的計(jì)數(shù)器都從零開(kāi)始(必須要寫(xiě)外層循環(huán)內(nèi),內(nèi)層循環(huán)外)
? ? 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('統(tǒng)計(jì)完成');
end;
/
舉報(bào)
Oracle數(shù)據(jù)庫(kù)高級(jí)開(kāi)發(fā)必備的基礎(chǔ),通過(guò)實(shí)例帶你熟練掌握
2 回答為什么我連接不了ORACLE?
2 回答為什么在end之后要加一個(gè)斜杠“/”啊
1 回答為什么每次我在developer上一運(yùn)行腳本就死掉?
1 回答老師我想問(wèn)一下,為啥psql新建連接是一直出錯(cuò)呀
2 回答最后為什么要測(cè)試?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
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;
? --外層循環(huán)
? loop
? ? fetch cdept into pdeptno;
? ? exit when cdept%notfound;
? ? --判斷部門是否存在,如果部門不存在直接退出所有循環(huán)
? ? select count(1) into flag from emp where deptno = pdeptno;
? ? if flag = 0 then return;
? ? end if;
? ? --第一層循環(huán)內(nèi)給變量賦值為0,保證每次內(nèi)層循環(huán)的計(jì)數(shù)器都從零開(kāi)始(必須要寫(xiě)外層循環(huán)內(nèi),內(nèi)層循環(huán)外)
? ? 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('統(tǒng)計(jì)完成');
end;
/