儲存過程三個例子

2021-07-07 02:58:06 字數 3629 閱讀 7950

/*

例項1:統計每年入職的員工個數。

可能sql:

select to_char(hiredate,』yyyy』) from emp;

*/ set serveroutput on

declare

cursor cemp is select to_char(hiredate,』yyyy』) from emp;

phiredate varchar2(4);

–計數器

count80 number := 0;

count81 number := 0;

count82 number := 0;

count87 number := 0;

begin

open cemp;

loop

–取乙個員工

fetch cemp into phiredate;

exit when cemp%notfound;

--判斷

if phiredate = '1980' then count80:=count80+1;

elsif phiredate = '1981' then count81:=count81+1;

elsif phiredate = '1982' then count82:=count82+1;

else count87 := count87+1;

end if;

end loop;

close cemp;

–輸出

dbms_output.put_line(『total:』||(count80+count81+count82+count87));

dbms_output.put_line(『1980:』|| count80);

dbms_output.put_line(『1981:』|| count81);

dbms_output.put_line(『1982:』|| count82);

dbms_output.put_line(『1987:』|| count87);

end;

//*

為員工長工資。從最低工資調起每人長10%,但工資總額不能超過5萬元,

請計算長工資的人數和長工資後的工資總額,並輸出輸出長工資人數及工資總額。

可能的sql:

員工: select empno,sal from emp order by sal;

長工資後的工資總額:1. 對sal進行累加: 新的工資總額=舊的工資 + sal*0.1;

2. sum(sal): 查詢資料庫

練習: 工資不能超過5w

*/ set serveroutput on

declare

–員工

cursor cemp is select empno,sal from emp order by sal;

pempno emp.empno%type;

psal emp.sal%type;

–長工資的人數

countemp number := 0;

–工資總額

saltotal number;

begin

–漲前工資總額

select sum(sal) into saltotal from emp;

open cemp;

loop

–工資總額》5w

exit when saltotal > 50000;

–取乙個員工

fetch cemp into pempno,psal;

exit when cemp%notfound;

--漲工資

update emp set sal=sal*1.1 where empno=pempno;

--人數

countemp := countemp +1;

--工資總額

saltotal := saltotal + psal * 0.1;

end loop;

close cemp;

commit;

–輸出

dbms_output.put_line(『長工資的人數:』|| countemp);

dbms_output.put_line(『工資總額:』|| saltotal);

end;

//*

用pl/sql語言編寫一程式,實現按部門分段(6000以上、(6000,3000)、3000元以下)

統計各工資段的職工人數、以及各部門的工資總額(工資總額中不包括獎金)

sql語句:

部門: select deptno from dept;

員工的工資: select sal from emp where deptno=???

工資總額: select sum(sal) from emp where deptno=???

*/ set serveroutput on

declare

–部門

cursor cdept is select deptno from dept;

pdno dept.deptno%type;

–部門中的員工

cursor cemp(dno number) is select sal from emp where deptno=dno;

psal emp.sal%type;

–各個段的人數

count1 number;count2 number;count3 number;

–部門的工資總額

saltotal number;

begin

open cdept;

loop

–取部門

fetch cdept into pdno;

exit when cdept%notfound;

--初始化

count1 :=0;count2:=0;count3:=0;

select sum(sal) into saltotal from emp where deptno=pdno;

--取部門中的員工

open cemp(pdno);

loop

fetch cemp into psal;

exit when cemp%notfound;

--判斷

if psal<3000 then count1:=count1+1;

elsif psal>=3000 and psal<6000 then count2:=count2+1;

else count3:=count3+1;

end if;

end loop;

close cemp;

--儲存當前部門

insert into msg1 values(pdno,count1,count2,count3,nvl(saltotal,0));

end loop;

close cdept;

commit;

dbms_output.put_line(『完成』);

end;

/

python sorted三個例子

例1.按照元素出現的次數來排序 seq 2,4,3,1,2,2,3 按次數排序 seq2 sorted seq,key lambda x seq.count x print seq2 4,1,3,3,2,2,2 改進 第一優先按次數,第二優先按值 seq3 sorted seq,key lambda...

colorlog的三個例子

import logging from logging.handlers import rotatingfilehandler from colorlog import coloredformatter 第一步 建立乙個日誌收集器logger logger logging.getlogger aut...

儲存過程幾個例子

create or replace procedure peace if is cursor var c is select from grade begin for temp in var c loop if temp.course name os then dbms output.put lin...