永发信息网

写一个存储过程,用游标循环每一个学生,每一次循环统计每一学生各个科目的成绩,打印到控制到输出如下

答案:2  悬赏:80  手机版
解决时间 2021-01-29 02:59
  • 提问者网友:一抹荒凉废墟
  • 2021-01-28 08:48
姓名 数学 语文 英语 总分
张三 90 56 88 234
李四 91 66 98 255
1. 创建如下表并初始化数据

学生表:
学生编号 数字类型 主键
学生姓名 字符类型

科目表
科目编号 数字类型 主键
科目名称姓名 字符类型

成绩表
成绩编号 数字类型 主键
成绩分数 数字类型,小数点保留1位
学生编号 数字类型 外键
科目编号 数字类型 外键

7创建序列

8针对学生表创建一个before insert 触发器,利用第7步创建的序列,实现对学生表主键的自动增长列

9 写一个存储过程,用游标循环每一个学生,每一次循环统计每一学生各个科目的成绩,打印到控制到输出如下
最佳答案
  • 五星知识达人网友:舊物识亽
  • 2021-01-28 09:07
--学生表
create table t_xsb (xsbh number(10),xsxm varchar2(300));
comment on table t_xsb is '学生表';
comment on column t_xsb.xsbh is '学生编号';
comment on column t_xsb.xsxm is '学生姓名';
alter table t_xsb add constraints pk_xsb primary key (xsbh) using index;
--科目表
create table t_kmb (kmbh number(10),kmmc varchar2(300));
comment on table t_kmb is '科目表';
comment on column t_kmb.kmbh is '科目编号';
comment on column t_kmb.kmmc is '科目名称';
alter table t_kmb add constraints pk_kmb primary key (kmbh) using index;
--成绩表
create table t_cjb
(cjbh number(10),
cjfs number(10,1),
xsbh number(10),
kmbh number(10));
comment on table t_cjb is '成绩表';
comment on column t_cjb.cjbh is '成绩编号';
comment on column t_cjb.cjfs is '成绩分数';
comment on column t_cjb.xsbh is '学生编号';
comment on column t_cjb.kmbh is '科目编号';
alter table t_cjb add constraints pk_cjb primary key (cjbh) using index;
alter table t_cjb add constraints fk_cjb_xsbh foreign key (xsbh)
references t_xsb(xsbh);
alter table t_cjb add constraints fk_cjb_kmbh foreign key (kmbh)
references t_kmb(kmbh);
--创建序列
create sequence sq_ls
increment by 1
start with 1000000000
maxvalue 9999999999
nocycle
nocache;
--创建学生表的before insert触发器,实现对学生表主键的自动增长列
create or replace trigger r_xsb
before insert on t_xsb
for each row
declare
v_xsbh number(10) := null;
begin
v_xsbh := sq_ls.nextval;
:new.xsbh := v_xsbh;
end;
--写一个存储过程,用游标循环每一个学生,
--每一次循环统计每一学生各个科目的成绩,打印到控制
create or replace procedure p_xscjtj is
v_xsbh number(10) := null;
v_xsxm varchar2(300) := null;
v_sx_nm number(10) := null;
v_yw_nm number(10) := null;
v_yy_nm number(10) := null;
v_zf_nm number(10) := null;
v_sx_ch varchar2(300) := null;
v_yw_ch varchar2(300) := null;
v_yy_ch varchar2(300) := null;
v_zf_ch varchar2(300) := null;
cursor c_xs is
select t.xsbh, t.xsxm from t_xsb t;
begin
dbms_output.enable(buffer_size => null);
open c_xs;
dbms_output.put_line(rpad(STR1 => '姓名',
PAD => ' ',
LEN => lengthb('姓名') + 5) ||
rpad(STR1 => '数学',
PAD => ' ',
LEN => lengthb('数学') + 5) ||
rpad(STR1 => '语文',
PAD => ' ',
LEN => lengthb('语文') + 5) ||
rpad(STR1 => '英语',
PAD => ' ',
LEN => lengthb('英语') + 5) ||
rpad(STR1 => '总分',
PAD => ' ',
LEN => lengthb('总分') + 5));
loop
fetch c_xs
into v_xsbh, v_xsxm;
exit when c_xs%notfound;
begin
select nvl(sum(case
when t2.kmmc = '数学' then
t1.cjfs
else
0
end),
0) sx,
nvl(sum(case
when t2.kmmc = '语文' then
t1.cjfs
else
0
end),
0) yw,
nvl(sum(case
when t2.kmmc = '英语' then
t1.cjfs
else
0
end),
0) yy,
nvl(sum(t1.cjfs), 0) zf
into v_sx_nm, v_yw_nm, v_yy_nm, v_zf_nm
from t_cjb t1, t_kmb t2
where t1.kmbh = t2.kmbh
and t1.xsbh = v_xsbh
group by t1.xsbh;
exception
when others then
v_sx_nm := 0;
v_yw_nm := 0;
v_yy_nm := 0;
v_zf_nm := 0;
end;
v_xsxm := rpad(STR1 => v_xsxm, PAD => ' ', LEN => lengthb('姓名') + 5);
v_sx_ch := rpad(STR1 => v_sx_nm, PAD => ' ', LEN => lengthb('数学') + 5);
v_yw_ch := rpad(STR1 => v_yw_nm, PAD => ' ', LEN => lengthb('语文') + 5);
v_yy_ch := rpad(STR1 => v_yy_nm, PAD => ' ', LEN => lengthb('英语') + 5);
v_zf_ch := rpad(STR1 => v_zf_nm, PAD => ' ', LEN => lengthb('总分') + 5);
dbms_output.put_line(v_xsxm || v_sx_ch || v_yw_ch || v_yy_ch ||
v_zf_ch);
end loop;
close c_xs;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
全部回答
  • 1楼网友:夜余生
  • 2021-01-28 10:38
updatedbo.分类set分类=@srt,总数=@asum看看这句,和你的意图相比,是否缺少了where?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯