项目生命周期:

瀑布模型

拿到一个项目后,首先:分析需要用到的SQL语句;

                      其次:分析需要定义的变量初始值是多少,怎么得到最终值;

案例一:

统计每年入职的员工数量以及总数量:

  SQL语句:select to_char(hiredate,'yyyy') from emp;--to_char(hiredate,'yyyy') :hiredate原本的格式是yyyy-mm-dd但是我们只需要年份,则强制转换为char型,并且格式为yyyy,强制转换语法:to_                       目的类型(目标字段,格式)

                    需要遍历整个表,那么需要用到光标->循环->退出条件:%notfound

      变量:phiredate varchar2(20)

               计数器:

                        count80 number := 0;

                        count82 number := 0;

                        count87 number := 0;

                        count89 number := 0;

set serveroutput on;  

declare

          cursor c is select  to_char(hiredate,'yyyy') from emp;--定义光标

          phiredate varchar2(20);

          count80 number := 0;

          count82 number := 0;

          count87 number := 0;

          count89 number := 0;

begin

          open c;--打开光标

          loop 

          fetch c into phiredate ;--取出员工的入职年份

          exit when c%notfound;--关闭循环的条件

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

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

          elsif  phiredate= '1987'  then   count87 number := count87+ 1;

          else     count87 number := count87+ 1;

          end if;

          end loop;

close c;

dbms_output.put_line('tatal:'||count80+count82+count87+count89)

end;

/

 案例二

  SQL语句:select no,sal from emp;

                    需要遍历整个表,那么需要用到光标->循环->退出条件:%notfound

      变量:pno emp.no%type;

               psal emp.sal%type;

               涨工资人数:countemp number := 0;

                涨后的工资总额:saltotal number;

                                       select sum(sal) into saltotal from emp;

                                        涨后的工资总额 = 涨前的工资总额 + sal*0.1;

set serveroutput on

declare 

cursor c is select empno,sal from order by sal;

pno emp.no%type;

psal emp.sal%type;

countemp number := 0;--涨工资的人数

saltatol number;--涨后的工作总额

begin

        select sum(sal) into saltatol from emp;--涨工资前的初始值

        open c;

        loop

              exit when saltotal >50000;

                    fetch c into pno,psal;

               exit when c%notfound;

                     update emp set sal := sal*1.1 where no = pno;--涨工资

                     countemp :=  countemp + 1;--人数+1

         end loop;

 close c;

commit;

dbms_output.out_put('人数= '||countepm||',涨后的工资总额:'||saltatol);

end:

/

 

 案例三

实现部门分段(6000分以上,6000》且3000《,3000以下)统计各工资段的职工人数,以及各部门的工资总额(不包括奖金)

首先需要创建存储结果的表msg:create table msg(deptno number;count1 number;count2 number;  count3 number;saltotal number);

set serveroutput on

declare

cursor cdept is select deptno from dept;--部门的光标

pdeptno dept.deptno %type;--部门光标对应的变量

cursor cemp(deptno) is select sal from emp where no = deptno;--部门工资的光标

psal emp.sal %type;--部门工资光标对应的变量

count1 number;count2 number;count3 number;--每个阶段员工计数器

begin

open cdept;

             loop

                   fetch cdept into pdeptno;--取出一个部门

                   exit when cdepy%notfound;

                   count1 :=0;count2 :=0;count3 :=0;--初始化计数器

                   select sum(sal) into saltatol from emp where deptno = pdeptno;--计算部门工资总额

                   open cemp(pdeptno)

                            loop

                                 fetch cemp into psal;--取出每一个员工的薪资

                                 exit when cemp%noufound;

                                 if psal <3000 then count1 := count1 +1;--判断工资范围

                                 elsif  psal>6000 then count3 := count3 +1;

                                 else count2 := count2 +1;

                                 end if;

                            end loop;

                    close cemp;

                    insert into  msg value(pdeptno,count1,count2,count3,saltatol);--保存当前部门的结果

            end loop;

close cdept;

end:

/

 

案例四

按照系分段统计(成绩小于60,大于85,中间段)课程为‘大学物理’各个分数段的学生人数,以及各系学生的平均成绩;

 

set serveroutput on

declare

cersor cdept is select dno ,dname from dep;

pdno dep.dno%type;

cursor cgrade(coursename varchar2,depno number) is select grade from sc where cno =  (select cno from course where cname = courname) and sno in (select sno from student where dno = depno)--成绩光标

pgrade sc.grade % type;

count1 number;count2 number;count3 number;--每个分数段人数计数器

avggrade number;--平均成绩

pcoursename varchar2 :='大学物理';

begin

open cdept;

loop

     fetch cdept into pdno,pdname;

     exit when cdept%notfound;

     count1 :=0;count2 :=0;count3 :=0;--初始化计数器

     select avg(grade) into avggrade from sc where cno =  (select cno from course where cname = courname) and sno in (select sno from student where dno = depno)

            open cgrade (pcoursename,pdno);

            loop

                 fetch cgrade into pgrade;

                 exit when cgrade %notfound;

                                 if psal <60 then count1 := count1 +1;--判断工资范围

                                 elsif  psal>85 then count3 := count3 +1;

                                 else count2 := count2 +1;

                                 end if;

                            end loop;

                 end loop;

                 close cgrade;

                 insert into msg value (pcoursename,count1,count2,count3,avggrade);

                 end loop;

  close cdept;

commit;

dbms_output.put_line('统计完成');

end:/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

         

 

posted on 2017-01-10 11:36  小狮子zzy  阅读(195)  评论(0编辑  收藏  举报