运用瀑布模型完成PL/SQL程序设计

版权声明:转载请注明! https://blog.csdn.net/cyl101816/article/details/79897034

    在通常的软件开发或者程序开发中都会运用到很多种模型,瀑布模型就是其中的一种。

    瀑布模型是将软件生命周期的各项活动规定为固定顺序的若干个阶段工作,最终得到软件产品。其核心思想就是化繁为简,采用结构化的分析设计方法实现逻辑。

     相对于软件开发,一般的程序开发涉及的开发阶段就简化了很多,但可以帮助我们迅速找准逻辑,快速完成程序设计。可以简化其设计阶段为:

     1.需求分析    分析程序的设计目的

     2.设计     包括概要设计和详细设计。概要设计指的是从程序的架构上描述程序的逻辑。详细设计则指的是具体到程序的具体功能。

     3.编码(coding)    

     4.测试(test)

     5.上线

    在程序设计的过程中,最忌讳的就是上来就编码,我们应该在编码之前完成程序的逻辑设计和具体的分析。在编写一个PL/SQL程序时,首先应该想到的是要用到哪一个SQL语句和程序设计中所需要的变量,变量的初始值是多少,变量的最终值如何得到。完成这样的一个步骤,在接下来的编码中就能够做到不出错,快速完成。

    案例1:统计每年入职的员工人数

/*
SQL语句
select to_char(hiredate,'yyyy') from emp;
-->查询的结果集,用到光标-->循环-->退出条件:notfound

变量:1初始值 2.如何得到
每年的入职员工人数
count80 number:=0;
count81 number:=0;
count82 number:=0;
count87 number:=0;
*/
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;
/                                 
    案例2: 涨工资问题,从工资最低的工作人员开始,没人涨10%,工资总额不能超过5w,返回涨工资的人数和涨后的工资总额
/*
SQL语句:
select empno,sal from emp order by sal;
-->光标——循环——退出条件:1.工资总额>5w 2.%notfound

变量:1.初始值 2.如何得到
涨工资的人数:
countemp number:=0;

涨后的工资总额:
total_sal number
1.select sum(sal) into total_sal from emp;
2.涨后的工资总额=涨前的工资总额+sal*0.1;
*/
declare
 cursor cemp is select sal,empno from emp order by sal;
 pempno emp.empno%type;
 psal emp.sal%type:=0;
 countemp number:=0;
 total_sal number;
 
begin 
 --得到总工资额
 select sum(sal) into total_sal from emp;

 --打开光标
 open cemp;
 loop
 
 exit when total_sal>50000;
 fetch cemp into psal,pempno;
 
 exit when cemp%notfound;
 update emp set sal=sal*(1+0.1) where empno=pempno;
 total_sal:=total_sal+psal*0.1;
 countemp:=countemp+1;
 --修正
 if total_sal>50000 then
    total_sal:=total_sal-psal*0.1;
    update emp set sal=sal/1.1 where empno=pempno;
    countemp:=countemp-1;
 end if;
 
 end loop;
 
 --关闭光标
 close cemp;
 
 commit;
 dbms_output.put_line('涨工资的人数:'||countemp);
 dbms_output.put_line('工资总额:'||total_sal);
end;
/


    













猜你喜欢

转载自blog.csdn.net/cyl101816/article/details/79897034