当前位置:文档之家› oracle作业(包)

oracle作业(包)

练习

定义一个包,使其中包括下面 功能:

1 建立过程,当传入学号和选课门数,首先判断SC_Number表是否存在,若不存在则创建该表格(包括学号和选修门数两列),
将传入值插入或修改到SC_Number表中(该生不存在则插入,若存在则修改其选课门数)(私有过程.

2 建立过程(重载),当用户输入学号(或姓名),课程号,成绩,将该信息插入到SC表格中,若该课程已经满额,则提示相
关信息;若该生已经选择了该课程,则修改该课程的成绩为输入成绩;若该生或该课程不存在,则提示相关错误。插入成功后
调用上一个过程将学生选课情况修改.
3 建立过程,当用户输入学号,将该生对应的选课信息(SC),学生基本信息(Student),SC_Number中关于该生的信息全部删除,
若该生不存在,则给出相关提示.

4 建立过程,实现删除SC_Number表格的功能.

测试代码:
declare
isSuccess varchar2(40);
begin

--测试重载add_sc1
pk_stu.add_sc('001','C01',99,isSuccess);
dbms_output.put_line(isSuccess);
pk_stu.add_sc('001','C04',89,isSuccess);
dbms_output.put_line(isSuccess);
pk_stu.add_sc('031','C02',87,isSuccess);
dbms_output.put_line(isSuccess);
pk_stu.add_sc('031','C92',88,isSuccess);
dbms_output.put_line(isSuccess);

dbms_output.put_line('');
--测试重载add_sc2
pk_stu.add_sc('张三','C24',80);
pk_stu.add_sc('张三','C54',85);
pk_stu.add_sc('张三1','C24',89);

--测试删除学号为001的学生的所有表数据
pk_stu.delete_stu('001');
--测试删除sc_number表
pk_stu.delete_sc_number;
end;


程序代码:

包头:
create or replace package pk_stu is

--当用户输入学号,课程号,成绩,插入学生成绩信息
procedure add_sc(asno in varchar2,
acno in varchar2,
agrade in number,
isSuccess out varchar2);

--当用户输入姓名,课程号,成绩,插入学生成绩信息
procedure add_sc(asname in varchar2, acno in varchar2, agrade in number);

--删除学生信息
procedure delete_stu(asno in varchar2);
--删除sc_number表
procedure delete_sc_number;
end pk_stu;

包体:
create or replace package body pk_stu is

procedure add_sc_number(asno in varchar2, coures_count in number) is
--判断sc_number表是否存在,1存在,0不存在
is_sc_number number;
--判断该生存不存在,1存在,0不存在
course_num number;
begin

begin
select 1 into is_sc_number from tab where trim(tname) = 'SC_NUMBER';
exception
when no_data_found then
is_sc_number := 0;
end;

--如果sc_number表不存在
if is_sc_number = 0 then
dbms_output.put_line('动态sql....');
execute immediate 'create table sc_number(sno varchar(10) prim

ary key,cnum number(3))';
execute immediate 'insert into sc_number select sno,count(*) from sc group by sno';
commit;
end if;

begin
--找该表中存不存在此学号的学生
execute immediate 'select 1 from sc_number where trim(sno)=:1'
into course_num
using trim(asno);
exception
when no_data_found then
course_num := 0;
when others then
dbms_output.put_line('系统错误...');
return;
end;

--若该表中存在此学号的学生
if (course_num = 1) then
begin
--修改选课门数
execute immediate 'update sc_number set cnum=:1 where trim(sno)=:2'
using coures_count, trim(asno);
commit;
exception
when others then
dbms_output.put_line('出现错误,更新失败...');
return;
end;
end if;

--若该表中不存在此学号的学生
if course_num = 0 then
begin
execute immediate 'insert into sc_number values(:1,:2)'
using asno, coures_count;
commit;
exception
when others then
dbms_output.put_line('出现错误,更新失败...');
return;
end;
end if;
end;

--向sc表中添加学生的选课信息1
procedure add_sc(asno in varchar2,
acno in varchar2,
agrade in number,
isSuccess out varchar2) is
tsno varchar2(20); --判断sc表中存不存在此记录
count_sc number; --sc表中选课的数目
max_course number; --course表中该课程的最大人数
temp number; --临时变量
stu_count number; --学生的选课数
begin

begin

select sno
into tsno
from sc
where trim(sno) = asno
and trim(cno) = acno;
exception
when no_data_found then
tsno := ' ';
when others then
isSuccess := '系统错误...';
return;
end;
-- dbms_output.put_line('****'||tsno||'****');
--存在该记录
if tsno != ' ' then
begin
--修改成绩
update sc
set grade = agrade
where trim(sno) = asno
and trim(cno) = acno;
commit;
isSuccess := '修改成功...';
exception
when others then
isSuccess := '修改失败...';
return;
end;
end if;

--不存在该记录
if tsno = ' ' then
begin
select snumber
into max_course
from course
where trim(cno) = trim(acno);
exception
when no_data_found then
isSuccess := '没有此课程号...';
return;
end;

select count(*) into count_sc from sc where trim(cno) = trim(acno);

if count_sc >= max_course then
isSuccess := '选课已满...';


return;
else
begin
select 1 into temp from student where trim(sno) = trim(asno);
exception
when no_data_found then
isSuccess := '没有此学生...';
return;
end;

--判断cno是否存在
begin
insert into sc values (asno, acno, agrade);
commit;
isSuccess := '插入成功...';
--找到该学生的选课门数
select count(*)
into stu_count
from sc
where trim(sno) = trim(asno);
--调用私有过程插入
add_sc_number(asno, stu_count);
exception
when others then
isSuccess := '插入失败...';
end;
end if;
end if;
end;

--向sc表中添加学生的选课信息2
procedure add_sc(asname in varchar2, acno in varchar2, agrade in number) is
isExist number; --判断sc表中存不存在此记录
count_sc number; --sc表中选课的数目
max_course number; --course表中该课程的最大人数
stu_sno varchar2(20); --该学生对应的学号
stu_count number; --学生的选课数
begin

begin

select 1
into isExist
from sc, student
where sc.sno = student.sno
and trim(https://www.doczj.com/doc/a615127378.html,o) = acno
and trim(student.sname) = asname;
exception
when no_data_found then
isExist := 0;
when others then
dbms_output.put_line('系统错误...');
return;
end;

--存在该记录
if isExist = 1 then
begin
--修改成绩
update sc
set grade = agrade
where sno in (select sc.sno
from sc, student
where sc.sno = student.sno
and trim(student.sname) = asname)
and trim(https://www.doczj.com/doc/a615127378.html,o) = trim(acno);
commit;
dbms_output.put_line('修改成功...');
exception
when others then
dbms_output.put_line('修改失败...');
return;
end;
end if;

--不存在该记录
if isExist = 0 then
begin
select snumber
into max_course
from course
where trim(cno) = trim(acno);
exception
when no_data_found then
dbms_output.put_line('没有此课程号...');
return;
end;

select count(*) into count_sc from sc where trim(cno) = trim(acno);

if count_sc >= max_course then
dbms_output.put_line('选课已满...');
return;
else
begin
select 1
into stu_sno
from student
where trim(sname) = trim(asname);
exception
when no_data_found then
dbms_output.put_line('没有此学生...');
return;
end;

--

判断cno是否存在
begin
insert into sc values (stu_sno, acno, agrade);
commit;
dbms_output.put_line('插入成功...');
--找到该学生的选课门数
select count(*)
into stu_count
from sc
where trim(sno) = trim(stu_sno);
--调用私有过程插入
add_sc_number(stu_sno, stu_count);
exception
when others then
dbms_output.put_line('插入失败...');
end;
end if;
end if;
end;

--删除学生信息
procedure delete_stu(asno in varchar2) is
temp number;

begin

begin
--查找该学号的学生存不存
select 1 into temp from student where trim(sno) = trim(asno);
exception
when no_data_found then
dbms_output.put_line('该学号的学生不存在...');
return;
end;

begin
--删除sc表中的该学号的学生的信息
delete from sc where trim(sno) = trim(asno);
dbms_output.put_line('sc表删除成功...');
--删除student表中该学号的学生的信息
delete from student where trim(sno) = trim(asno);
dbms_output.put_line('student表删除成功...');

begin
execute immediate 'delete from sc_number where trim(sno)=:1'
using trim(asno);
dbms_output.put_line('sc_number表删除成功...');
exception
when others then
dbms_output.put_line('系统错误...');
end;

commit;
end;
end;

--删除sc_number表
procedure delete_sc_number is
is_sc_number number;
begin

begin
--判断sc_number是否存在
select 1 into is_sc_number from tab where tname = 'SC_NUMBER';

if is_sc_number = 1 then
execute immediate 'drop table sc_number';
dbms_output.put_line('sc_number表删除成功...');
end if;
end;
end;

end pk_stu;

相关主题
文本预览
相关文档 最新文档