给下面的代码纠错:create procedure student_score_by_name(in v_sname varchar(20)) begin declare v_sno,v_cno varchar(20); declare v_grade int; declare v_level varchar(10); declare flag int default 1; declare c1 cursor for select sc.sno,cno,grade, level from student,sc where student.sno=sc.sno and sname=v_sname; declare continue handler for not found set flag=0; open c1; while flag do fetch c1 into v_sno,v_cno,v_grade,v_level; if isnull(v_level) then if v_grade between 85 and 100 then set level='A'; elseif v_grade between 75 and 84 then set level='B'; elseif v_grade between 65 and 74 then set level='C'; elseif v_grade between 60 and 64 then set level='D'; else set level='F'; end if; update sc set level=v_level where sno=v_sno and cno=v_cno; end if; end while; close c1; end;
时间: 2024-01-22 07:02:01 浏览: 74
下面是已经纠错的代码:
```
CREATE PROCEDURE student_score_by_name(in v_sname varchar(20))
BEGIN
DECLARE v_sno, v_cno varchar(20);
DECLARE v_grade int;
DECLARE v_level varchar(10);
DECLARE flag int default 1;
DECLARE c1 CURSOR FOR SELECT sc.sno, cno, grade, level FROM student, sc
WHERE student.sno = sc.sno AND sname = v_sname;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 0;
OPEN c1;
WHILE flag DO
FETCH c1 INTO v_sno, v_cno, v_grade, v_level;
IF isnull(v_level) THEN
IF v_grade BETWEEN 85 AND 100 THEN SET v_level = 'A';
ELSEIF v_grade BETWEEN 75 AND 84 THEN SET v_level = 'B';
ELSEIF v_grade BETWEEN 65 AND 74 THEN SET v_level = 'C';
ELSEIF v_grade BETWEEN 60 AND 64 THEN SET v_level = 'D';
ELSE SET v_level = 'F';
END IF;
UPDATE sc SET level = v_level WHERE sno = v_sno AND cno = v_cno;
END IF;
END WHILE;
CLOSE c1;
END;
```
主要的问题是:
1. 缺少 BEGIN 和 END,需要在创建存储过程时加上。
2. CURSOR 声明语句中 SELECT 语句缺少表别名。
3. isnull() 函数应该写成 IS NULL。
4. 在 IF 语句中,变量名应该是 v_level,而不是 level。
5. 在 END IF 和 END WHILE 后面需要加上分号。
阅读全文