统计每年入职的员工人数:(利用emp表中的 hiredate 字段)
1 set SERVEROUTPUT ON 2 declare 3 --定义光标 4 cursor cemp is select to_char(hiredate, 'yyyy') from emp; 5 phiredate varchar2(4); 6 --每年入职的员工人数 7 count80 number:=0; 8 count81 number:=0; 9 count82 number:=0; 10 count87 number:=0; 11 12 begin 13 --打开光标 14 open cemp; 15 loop 16 --取出一个员工的入职年份 17 fetch cemp into phiredate; 18 exit when cemp%notfound; 19 20 --判断入职年份 21 if 22 phiredate='1980' then count80:=count80+1; 23 elsif phiredate='1981' then count81:=count81+1; 24 elsif phiredate='1982' then count82:=count82+1; 25 else count87:=count87+1; 26 end if; 27 28 end loop; 29 --关闭光标 30 close cemp; 31 32 --输出结果 33 dbms_output.put_line('Total:'||(count80+count81+count82+count87)); 34 dbms_output.put_line('1980:'||count80); 35 dbms_output.put_line('1981:'||count81); 36 dbms_output.put_line('1982:'||count82); 37 dbms_output.put_line('1987:'||count87); 38 end; 39 /
结果: