[SQL] CREATE PROCEDURE proc_10ASBEGIN -- 添加序号字段 ALTER TABLE 学生成绩表 ADD 序号 INT; -- 添加评价字段 ALTER TABLE 学生成绩表 ADD 评价 VARCHAR(20); -- 更新评价字段 DECLARE @id INT; DECLARE @score INT; DECLARE @evaluation VARCHAR(20); DECLARE cursor1 CURSOR FOR SELECT id, score FROM 学生成绩表; OPEN cursor1; FETCH NEXT FROM cursor1 INTO @id, @score; WHILE @@FETCH_STATUS = 0 BEGIN IF @score >= 90 SET @evaluation = '优秀'; ELSE IF @score >= 80 SET @evaluation = '良好'; ELSE IF @score >= 70 SET @evaluation = '一般'; ELSE IF @score >= 60 SET @evaluation = '合格'; ELSE SET @evaluation = '较差'; UPDATE 学生成绩表 SET 序号 = @id, 评价 = @evaluation WHERE id = @id; FETCH NEXT FROM cursor1 INTO @id, @score; END CLOSE cursor1; DEALLOCATE cursor1;END [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
时间: 2023-12-06 17:40:38 浏览: 185
这段 SQL 存储过程的语法错误可能是因为 BEGIN 后面没有加空格,正确的写法应该是:
CREATE PROCEDURE proc_10
AS
BEGIN
-- 添加序号字段
ALTER TABLE 学生成绩表 ADD 序号 INT;
-- 添加评价字段
ALTER TABLE 学生成绩表 ADD 评价 VARCHAR(20);
-- 更新评价字段
DECLARE @id INT;
DECLARE @score INT;
DECLARE @evaluation VARCHAR(20);
DECLARE cursor1 CURSOR FOR SELECT id, score FROM 学生成绩表;
OPEN cursor1;
FETCH NEXT FROM cursor1 INTO @id, @score;
WHILE @@FETCH_STATUS = 0
BEGIN
IF @score >= 90
SET @evaluation = '优秀';
ELSE IF @score >= 80
SET @evaluation = '良好';
ELSE IF @score >= 70
SET @evaluation = '一般';
ELSE IF @score >= 60
SET @evaluation = '合格';
ELSE
SET @evaluation = '较差';
UPDATE 学生成绩表 SET 序号 = @id, 评价 = @evaluation WHERE id = @id;
FETCH NEXT FROM cursor1 INTO @id, @score;
END
CLOSE cursor1;
DEALLOCATE cursor1;
END
阅读全文