1.功能设计
某高校开发的研究生招生系统,要求设计pl/sql程序对考生的成绩数据进行处理,处理的逻辑是根据每门专业课的最低分数线和总分的最低分数线自动将考生归类为录取考生,调剂考生和落选考生。
为此设计两个数据表,graduate数据表存放考生成绩,resut数据表存放处理结果,pl/sql程序完成的功能就是将graduate数据表的数据逐行扫描,根据分数线进行判断,计算出各科总分,在result数据表中将标志字段自动添加上"录取"或"落选"。
2.数据表设计
--研究生表--
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
);
--结果表--
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,
"TOTALSCORE" NUMBER(5,1) NOT NULL,
"FLAG" VARCHAR2(4) NOT NULL
);
3.录入数据
insert into graduate values(2003080520,'张三丰','硕士',55,56,67,78,89);
insert into graduate values(2003060555,'张翠山','硕士',66,78,78,89,92);
insert into graduate values(2003056066,'张无忌','硕士',76,67,89,90,66);
insert into graduate values(2003010989,'赵敏','硕士',45,59,74,66,56);
insert into graduate values(2003050677,'周芷若','硕士',77,67,72,87,66);
insert into graduate values(2003869401,'小昭','硕士',56,67,56,64,34);
insert into graduate values(2003340987,'阿离','硕士',68,93,64,80,56);
insert into graduate values(2003056709,'宋远桥','硕士',90,68,81,61,67);
insert into graduate values(2003100894,'殷素素','硕士',69,73,62,70,75);
4.程序设计
/*定义过程参数*/
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.totalscore%type
)
as
/*定义graduaterecord为记录型变量,临时存放通过游标从graduate数据表中提取的记录*/
graduaterecord graduate%rowtype;
/*定义graduatetotalscore为数值型变量,统计总分*/
graduatetotalscore result.totalscore%type;
/*定义graduateflag为字符型变量,根据结果放入"落选"或"录取",然后写入数据表result*/
graduateflag varchar2(4);
/*定义游标graduatecursor,存放的是所有的graduate数据表中的记录*/
cursor graduatecursor is select * from graduate;
/*定义异常处理errormessage*/
errormessage exception;
/*开始执行*/
begin
/*打开游标*/
open graduatecursor;
/*如果游标没有数据,激活异常处理*/
if graduatecursor%notfound then
raise errormessage;
end if;
/*游标有数据,指针指向第一条记录,每执行fetch命令,就自动下移,循环执行到记录提取完毕为止*/
loop
fetch graduatecursor into graduaterecord;
/*计算总分*/
graduatetotalscore := graduaterecord.yingyu + graduaterecord.zhengzhi + graduaterecord.zhuanye1
+ graduaterecord.zhuanye2 + graduaterecord.zhuanye3;
/*判断单科和总分是否满足录取要求,若满足,graduateflag变量值为"录取",否则为"落选"*/
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;
/*向结果数据表result中插入处理后的数据*/
insert into result values(graduaterecord.bh,graduaterecord.xm,graduaterecord.lb,
graduaterecord.yingyu,graduaterecord.zhengzhi,graduaterecord.zhuanye1,graduaterecord.zhuanye2,
graduaterecord.zhuanye3,graduatetotalscore,graduateflag);
end loop;
/*关闭游标*/
close graduatecursor;
/*提交结果*/
commit;
/*异常处理,提示错误信息*/
exception
when errormessage then
dbms_output.put_line('无法打开数据表');
/*程序执行结束*/
end;
5.执行结果,程序调用。
/*定义6个入口变量,分别对应graduate数据表中的专业课和总分分数线*/
declare
score1 number(4,1);
score2 number(4,1);
score3 number(4,1);
score4 number(4,1);
score5 number(4,1);
scoretotal number(5,1);
/*将分数线赋值,在这里修改各值就代表不同的分数线*/
begin
score1 := 50;
score2 := 56;
score3 := 60;
score4 := 62;
score5 := 64;
scoretotal := 325;
/*调用处理过程*/
graduateprocess(score1,score2,score3,score4,score5,scoretotal);
end;
综合运用pl/sql的设计要素,就可以设计出很多复杂的处理程序,这也是DBA的一项重要任务。