有两张表Student(sno,sname,sex,age,class,department),SC(sno,cno,writtenScore,LabScore) 创建存储过程,cno='Dep04_b001'本次考试平均分以及未通过考试的学员名单。(笔试成绩满分 80 分,上机成绩满分 20 分,双科成绩均达到满分的80%以上为优秀, 单科不足满分的60%为不合格否则为合格)( 用带有游标的存储过程)
时间: 2024-02-12 09:05:51 浏览: 107
好的,以下是根据您提供的表和要求编写的存储过程:
```sql
CREATE PROCEDURE get_exam_result
@cno varchar(20)
AS
BEGIN
DECLARE @avg_score float
DECLARE @sno varchar(20)
DECLARE @written_score int
DECLARE @lab_score int
DECLARE @total_score float
DECLARE @pass_count int
DECLARE @fail_count int
DECLARE @name varchar(20)
DECLARE cur_exam_result CURSOR FOR
SELECT SC.sno, SC.writtenScore, SC.LabScore, Student.sname
FROM SC
JOIN Student ON SC.sno = Student.sno
WHERE SC.cno = @cno
SET @pass_count = 0
SET @fail_count = 0
SET @total_score = 0
OPEN cur_exam_result
FETCH NEXT FROM cur_exam_result INTO @sno, @written_score, @lab_score, @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @total_score = @written_score * 0.8 + @lab_score * 0.2
IF (@total_score >= 80)
BEGIN
SET @pass_count = @pass_count + 1
END
ELSE
BEGIN
IF (@written_score >= 64 AND @lab_score >= 16 AND @total_score >= 0.6 * 100)
BEGIN
SET @pass_count = @pass_count + 1
END
ELSE
BEGIN
SET @fail_count = @fail_count + 1
PRINT '不合格学员:' + @name
END
END
SET @avg_score = @avg_score + @total_score
FETCH NEXT FROM cur_exam_result INTO @sno, @written_score, @lab_score, @name
END
CLOSE cur_exam_result
DEALLOCATE cur_exam_result
SET @avg_score = @avg_score / (@pass_count + @fail_count)
PRINT '本次考试平均分:' + CONVERT(varchar(20), @avg_score)
PRINT '未通过考试的学员人数:' + CONVERT(varchar(20), @fail_count)
END
```
使用方法:
```sql
EXEC get_exam_result 'Dep04_b001'
```
注意:这个存储过程的实现方式可能不是最优的,仅供参考。同时,游标的使用需要谨慎,如果表的数据量较大,可能会对性能产生影响。
阅读全文