SQL存储过程学习

一、功能设计

开发目标研究生招生系统,要求设计PL/SQL程序对考生的成绩数据进行处理,处理的逻辑是根据每门专业课的最低分数线和总分的最低分数线自动将考生归类为录取考生、调剂考生、落选考生。
为此设计2个数据表,graduate数据表存放考生成绩,result数据表存放处理结果,PL/SQL程序完成的功能就是将graduate数据表中的数据逐行扫描,根据分数线进行判断,计算各科总分,在result数据表中将标志字段自动添加上“录取”或“落选”。

二、数据表设计

create table RESULT
(
  BH        NUMBER(10) not null,
  XM        VARCHAR2(10) not null,
  LB        VARCHAR2(10) not null,
  YINGYU    NUMBER(4,1) not null,
  ZHENGZHI  NUMBER(4,1) not null,
  ZHUANYE1  NUMBER(4,1) not null,
  ZHUANYE2  NUMBER(4,1) not null,
  ZHUANYE3  NUMBER(4,1) not null,
  TOTALSORE NUMBER(5,1) not null,
  FLAG      VARCHAR2(4) not null
)
create table GRADUATE
(
  BH       NUMBER(10) not null,
  XM       VARCHAR2(10) not null,
  LB       VARCHAR2(10) not null,
  YINGYU   NUMBER(4,1) not null,
  ZHENGZHI NUMBER(4,1) not null,
  ZHUANYE1 NUMBER(4,1) not null,
  ZHUANYE2 NUMBER(4,1) not null,
  ZHUANYE3 NUMBER(4,1) not null
)


insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003080520, '张三丰', '硕士', 55, 56, 67, 78, 89);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003060555, '张翠山', '硕士', 66, 78, 78, 89, 92);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003056066, '张无忌', '硕士', 76, 67, 89, 90, 66);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003010989, '赵敏', '硕士', 45, 59, 74, 66, 56);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003050677, '周芷若', '硕士', 77, 67, 72, 87, 66);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003869401, '小昭', '硕士', 56, 67, 56, 64, 34);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003340987, '阿离', '硕士', 68, 93, 64, 80, 56);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003056709, '宋元桥', '硕士', 90, 68, 81, 61, 67);
insert into GRADUATE (BH, XM, LB, YINGYU, ZHENGZHI, ZHUANYE1, ZHUANYE2, ZHUANYE3)
values (2003100894, '殷素素', '硕士', 69, 73, 62, 70, 75);


三、程序设计

CREATE OR REPLACE Procedure graduateprocess
(
  tempzhengzhi In graduate.zhengzhi%type,
  tempyingyu In graduate.yingyu%type,
  tempzhuanye1 In graduate.zhuanye1%type,
  tempzhuanye2 In graduate.zhuanye2%type,
  tempzhuanye3 In graduate.zhuanye3%type,
  temptotalscore In result.totalsore%type
)

as
/* 定义graduaterecord为记录型变量,临时存放通过游标从graduate表中提取的记录*/
  graduaterecord graduate%rowtype;
 /* 定义graduatetotalscore为数值型变量,统计总分 */
  graduatetotalscore result.totalsore%type;
 /* 定义graduateflag为字符变量,根据结果放入落选或录取,然后写入数据表result */
  graduateflag varchar2(4);
 /*定义游标*/
  cursor graduatecursor is
  select * From graduate;
  errormessage exception;
begin
    open graduatecursor;
    if graduatecursor%notfound then
      raise errormessage;
    end if;
    loop
      fetch graduatecursor into graduaterecord;
      graduatetotalscore:=graduaterecord.yingyu + graduaterecord.zhengzhi + graduaterecord.zhuanye1 + graduaterecord.zhuanye2 + graduaterecord.zhuanye3;
      if (graduaterecord.yingyu >= tempyingyu and
          graduaterecord.zhengzhi >= tempzhengzhi and
          graduaterecord.zhuanye1 >= tempzhuanye1 and
          graduaterecord.zhuanye2 >= tempzhuanye2 and
          graduaterecord.zhuanye3 >= tempzhuanye3 and
          graduatetotalscore >= temptotalscore) then
          graduateflag:='录取';
      else
          graduateflag:='落选';
      end if;

      exit when graduatecursor%notfound;
      insert into
        result(bh,xm,lb,zhengzhi,yingyu,zhuanye1,zhuanye2,zhuanye3,totalsore,flag)
        values(graduaterecord.bh,graduaterecord.xm,graduaterecord.lb,graduaterecord.zhengzhi,graduaterecord.yingyu,graduaterecord.zhuanye1,graduaterecord.zhuanye2,graduaterecord.zhuanye3,graduatetotalscore,graduateflag);
      end loop;
    close graduatecursor;
    commit;
    Exception
    Rollback;
    when errormessage then
    dbms_output.put_line('无法打开数据表,请联系管理员!');
end;


你可能感兴趣的:(sql)