创建存储过程proc1,输入某同学的学号,使用游标统计该同学的平均分,并返回平均分,同时显示该同学的姓名、课程名称和选课成绩。
时间: 2024-06-14 18:04:00 浏览: 132
```sql
CREATE PROCEDURE proc1(IN student_id INT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE sName VARCHAR(255);
DECLARE cName VARCHAR(255);
DECLARE score INT;
DECLARE cur CURSOR FOR
SELECT student_name, course_name, course_score
FROM student_courses
WHERE student_id = student_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
read_loop: LOOP
FETCH cur INTO sName, cName, score;
IF done THEN
LEAVE read_loop;
END IF;
-- 逐行显示同学的姓名,课程名,成绩
SELECT sName, cName, score;
END LOOP;
CLOSE cur;
-- 统计该同学的平均分并返回
SELECT AVG(course_score) AS average_score
FROM student_courses
WHERE student_id = student_id;
END;
```
阅读全文