【摘要】
create table 部门
(
部门号 char(10),
名称 char(10),
经理名 char(10),
电话 char(11),
primary key(部门号)
);create table employee(
职工号 char(10),
姓名 char(10),
年龄 smallint check(年龄<=60),…
create table 部门
(
部门号 char(10),
名称 char(10),
经理名 char(10),
电话 char(11),
primary key(部门号)
);
create table employee(
职工号 char(10),
姓名 char(10),
年龄 smallint check(年龄<=60),
职务 char(10),
工资 smallint,
部门号 char(10),
primary key(职工号),
foreign key(部门号) references 部门(部门号)
);
create table gradeL
( levelx char(10),
cnt int
);
insert into gradeL
values('[0,60]',0),('[60,80]',0),('[80,90]',0),('[90,100]',0);
select * from gradel;
先建一个表
create procedure proc_grade
@cname char(9)
as
begin declare
@less60 int,
@60to80 int,
@80to90 int,
@morethan90 int,
@cno char(4);
select @cno = cno from course where cname=@cname;
select @less60 = count(*) from sc where grade<60 and cno=@cno;
update gradeL set cnt=@less60 where levelx='[0,60]';
select @60to80 =count(*) from sc where grade>=60 and grade<80 and Cno = @Cno;
update gradeL set cnt=@60to80 where levelx='[60,80]';
select @80to90 =count(*) from sc where grade>=80 and grade<90 and Cno = @Cno;
update gradeL set cnt=@60to80 where levelx='[80,90]';
select @morethan90 =count(*) from sc where grade>=90 and Cno = @Cno;
update gradeL set cnt=@morethan90 where levelx='[90,100]';
end;
exec proc_grade
@CNAME='离散数学';
select * from gradeL;
同样的 用这个也是可以查询其他课程的
create procedure proc_avegrade
@cname char(10)
as
begin
declare @cno char(4),
@ave smallint;
select @cno = cno from course where cname=@cname;
select @ave=avg(grade) from sc where cno=@cno;
print @cname+'的平均成绩为'+@ave;
end;
执行的时候失败了 才想起需要转换数据
print @cname+'的平均成绩为'+convert(varchar,@ave);
这一行改成这样
成功执行
先为学生表加上评级这个属性
alter table sc add level char(1);
create procedure gradelevel
as begin
update SC set level = 'A' where Grade >= 95;
update SC set level = 'B' where Grade >= 80 and Grade < 95;
update SC set level = 'C' where Grade >= 60 and Grade < 80;
update SC set level = 'D' where Grade >= 40 and Grade < 60;
update SC set level = 'E' where Grade < 40;
end;
exec gradelevel;
select * from sc;
体会:
这几个题 确实做得不是很快 但是对存储过程的熟练度似乎得到了提升
觉得这一块的东西 挺有意思的
文章来源: blog.csdn.net,作者:’ or 1=1 ;,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_45782091/article/details/116002113
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END