数据库实验六

-- 1.	用函数实现:求某个专业选修了某门课程的学生人数,
--并调用函数求出计算机系“数据库”课程的选课人数。
create function num_course_dept(@dept as char(2),@cname as char(10))
returns int
as
BEGIN
declare @num INT
select @num=count(*)
from sc,student,course
where sc.Sno=student.Sno and Sdept=@dept and course.Cname=@cname and course.Cno=sc.Cno
return @num
END

select dbo.num_course_dept('CS','数据库') 人数


-- 2.	用内嵌表值函数实现:查询某个专业所有学生所选的每门课的平均成绩;
--调用该函数求出计算机系的所有课程的平均成绩。
create function avg_dept_sc(@dept as char(4))
returns table
as
return(select Cno,avg(Grade) 平均成绩
from sc,student
where sc.Sno=student.Sno and Sdept=@dept
group by Cno
)

select * from dbo.avg_dept_sc('CS')


-- 3.	创建多语句表值函数,通过学号作为实参调用该函数,可显示该学生的姓名以及各门课的成绩和学分,
--调用该函数求出“200515002”的各门课成绩和学分。
create function stu_score_credit (@sno as char(10))
returns @score_xf table(
    Sname char(6),
	Score int,
	Ccredit int
)
as
begin
insert @score_xf
select Sname,Grade,Ccredit
from sc,student,course
where sc.Sno=student.Sno and sc.Cno=course.Cno and student.Sno=@sno
return
end

select *
from dbo.stu_score_credit('200515002')


-- 4.	编写一个存储过程,统计某门课程的优秀(90-100)人数、良好(80-89)人数、中等(70-79)人数、及格(60-69)人数和及格率,
--其输入参数是课程号,输出的是各级别人数及及格率,及格率的形式是90.25%,执行存储过程,在消息区显示1号课程的统计信息。
create procedure rank_stu @cno char(10)
as
select count(case when Grade>=90 then 1 end) 优秀,count(case when Grade<90 and Grade>=80 then 1 end) 良好,
		count(case when Grade>=70 and Grade<79 then 1 end) 中等,count(case when Grade>=60 and Grade<70 then 1 end) 及格,
		concat(convert(float,count(case when Grade>=60 then 1 end))/convert(float,count(distinct sc.Sno))*100,'%') 及格率
from sc
where Cno=@cno

exec rank_stu 1

-- 5.	创建一个带有输入参数的存储过程,该存储过程根据传入的学生名字,查询其选修的课程名和成绩,
--执行存储过程,在消息区显示方可以的相关信息。
create procedure stu_cname_score @sname char(6)
as
select Cname,Grade
from sc,student,course
where sc.Sno=student.Sno and Sname=@sname and sc.Cno=course.Cno

exec stu_cname_score 方可以

-- 6.	以基本表 course或者kc为基础,完成如下操作
-- 生成显示如下报表形式的游标:报表首先列出学生的学号和姓名,然后在此学生下,列出其所选的全部课程的课程号、课程名和学分;
--依此类推,直到列出全部学生。
declare @sno1 char(10),@sname char(6),@sno2 char(10),@cno char(10),@cname char(10),@credit int
declare xs cursor
for select Sno,Sname
from student
open xs
fetch next from xs into @sno1,@sname
while @@FETCH_STATUS=0
begin
    print @sno1+' '+@sname
	declare xk cursor
	for select sc.Sno,sc.Cno,Cname,Ccredit
	from course,sc
	where course.Cno=sc.Cno
	open xk
	fetch next from xk into @sno2,@cno,@cname,@credit
	while @@FETCH_STATUS=0
	begin
	    if @sno1=@sno2
		   print @cno+@cname+convert(char(6),@credit)
	fetch next from xk into @sno2,@cno,@cname,@credit
	end
	close xk
	deallocate xk
fetch next from xs into @sno1,@sname
end
close xs
deallocate xs



-- 7.	请设计一个存储过程实现下列功能:判断某个专业某门课程成绩排名为n的学生的成绩是否低于该门课程的平均分,如果低于平均分,
--则将其成绩改为平均分,否则输出学号、姓名、班号、课程号、课程名、成绩。(提示:可以在存储过程内部使用游标)。
create procedure judge @dept char(4),@cno char(10),@n int
as
declare @sno char(10),@sgrade int,@avg_sgrade int,@sname char(6)

declare cj cursor scroll
for select sc.Sno,Grade,Sname
from sc,student
where sc.Cno=@cno and student.Sdept=@dept
order by Grade desc

select @avg_sgrade=(select avg(Grade)
		from sc
		where sc.Cno=@cno)

open cj
fetch absolute @n from cj into @sno,@sgrade,@sname
	if(@sgrade>@avg_sgrade)
		print @sno+' '+@sname+' '+@cno+' '+convert(char(6),@sgrade)
	else
	    update sc
		set sc.Grade=@avg_sgrade
		where sc.Sno=@sno and sc.Cno=@cno

exec judge CS,1,1

 

你可能感兴趣的:(数据库sql)