mySQL查询函数的语法错误,将小数等级转换为字母等级

时间:2010-03-04 22:21:27

标签: mysql syntax-error mysql-error-1064

尝试在MySQL中创建函数以将小数等级转换为字母等级时,我遇到语法错误的麻烦。

可能是什么问题?

  

错误1064(42000):您的SQL语法有错误;检查与mySQL服务器版本对应的手册,以便在'conv(gpa DOUBLE)附近使用正确的语法   退货CHAR   BEGIN
  DECLARE等级CHAR;
  在第1行的gp'中选择案例

这是我的代码:

delimiter $

CREATE FUNCTION conv(gpa DOUBLE) 
RETURNS CHAR
BEGIN 

DECLARE grade CHAR; 

SELECT CASE WHEN gpa BETWEEN 3.5 AND 4.0 THEN 'A' 
WHEN gpa BETWEEN 3.0 AND 3.4 THEN 'B' 
WHEN gpa BETWEEN 2.5 AND 2.9 THEN 'C' 
WHEN gpa BETWEEN 2.0 AND 2.4 THEN 'D' 
ELSE 'F' 
END INTO grade 

RETURN grade;

END $

1 个答案:

答案 0 :(得分:3)

你在结尾附近错过了一个分号:

SELECT CASE WHEN gpa BETWEEN 3.5 AND 4.0 THEN 'A' 
WHEN gpa BETWEEN 3.0 AND 3.4 THEN 'B' 
WHEN gpa BETWEEN 2.5 AND 2.9 THEN 'C' 
WHEN gpa BETWEEN 2.0 AND 2.4 THEN 'D' 
ELSE 'F' 
END INTO grade;

此外,您可能不应使用名称conv,因为这已经意味着something else