第10次实验2012-答案
- 格式:docx
- 大小:28.50 KB
- 文档页数:4
1、编制一个函数fc_avgp,根据输入的民族,返回该类别学生的平均年龄,并输入实参调用该函数。
create function fc_avgp(@na char(10))
returns real
as
begin
declare@pjnl real
select@pjnl=avg(age)
from student
where nation=@na
return@pjnl
end
select dbo.fc_avgp('汉族')
2、编制一个函数fc_count,输入年龄,返回该年龄的学生人数,并输入实参调用该函数。
create function fc_count(@age int)
returns int
as
begin
declare@count int
select@count=count(*)
from student
where age=@age
return@count
end
select dbo.fc_count(20)
3、定义两个变量分别存放成绩(sc表里的score列)上限和下限,根据给定的两个变量的值,返回在该成绩范围内的所有学生的信息(姓名、性别、民族、课号、成绩)。
declare@up int
declare@low int
select@low=60,@up=80
select姓名=name,性别=sex,民族=nation,课号=courseid,成绩=score
from student s join sc on s.id=sc.id
where score between@low and@up
4、定义变量“@tsmc”,将某课程的名称赋值给该变量,查询出该课程的选课情况。
如果没有选课的学生,则显示“该课程无人选修”,否则,显示选修该课程的学号、姓
名。
declare@tsmc as char(20)
set@tsmc='网络'
if(
select count(*)
from sc,course
where course.coursename=@tsmc and sc.courseid=course.courseid
)
>0
begin
select s.id,
from student s,sc,course c
where s.id=sc.id and sc.courseid=c.courseid and c.coursename=@tsmc end
else
begin
select'该课程无人选修'
end
declare@tsmc as char(20)
declare@n int
set@tsmc='网络'
select@n=count(*)
from sc,course
where course.coursename=@tsmc and sc.courseid=course.courseid
if@n>0
begin
select s.id,
from student s,sc,course c
where s.id=sc.id and sc.courseid=c.courseid and c.coursename=@tsmc end
else
begin
select'该课程无人选修'
end
5、修改表student结构,增加列“入学成绩”,列名为:score,类型为int,并给该列赋值。
调整所有学生的入学成绩,如果入学成绩不超过450,则各增加10%,并采用向上取整;如果超过450本但不超过480,则各增加5%,采用向上取整;如果超过480但不超过500,则各增加3%,并采用向下取整;如果超过500,则增加1%,并采用向
下取整。
alter table student add score int
update student set score=440 where id=201541030101
update student set score=445 where id=201541030102
update student set score=450 where id=201541030103
update student set score=455 where id=201541030104
update student set score=460 where id=201541030105
update student set score=475 where id=201541030106
update student set score=481 where id=201541030107
update student set score=490 where id=201541030108
update student set score=420 where id=201541030109
update student set score=478 where id=201541030110
update student
set score=(case
when score<= 450
then ceiling(score* 1.1)
when score<=480
then ceiling(score*1.05)
when score<=500
then floor(score*1.03)
else
floor(score*1.01)
end)
6、判定sc表是否有成绩不及格的学生,如果有,则将所有学生的成绩+10,直到所有学生的成绩都大于或等于60或者有学生的成绩超过100时停止,然后等待3秒后,显示出所有sc表信息。
while (select min(score)from sc)<60 and(select max(score)from sc)<=100 begin
update sc
set score=score+10
end
go
waitfor delay'0:0:3'
select*
from sc
7、查询学号为“201541030101”的学生的平均分是否超过了85分,若超过则输出“X X 考出了高分”,否则输出“XX 考的一般”。
注意XX要换成具体名字。
declare@cno as char(12)
declare@cna as char(8)
declare@avg as real
set@cno='201541030101'
select@cna=name from student where id=@cno select@avg=avg(score)from sc where id=@cno if@avg>85
select@cna+'考出了高分'
else
select@cna+'考得很一般'。