mySQL 查询函数将小数成绩转换为字母成绩时出现语法错误

发布于 2024-08-23 20:30:44 字数 599 浏览 7 评论 0原文

当我尝试在 MySQL 中创建一个函数将小数成绩转换为字母成绩时,我遇到了语法错误。

可能是什么问题?

错误 1064 (42000):您的 SQL 语法有错误;检查与您的 mySQL 服务器版本相对应的手册,了解在 'conv(gpa DOUBLE) 附近使用的正确语法 返回字符 开始
声明等级 CHAR;
SELECT CASE WHEN gp' 在第 1 行

这是我的代码:

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 $

I'm having troubles with a syntax error when trying to create a function in MySQL to convert a decimal grade into a letter grade.

What could be the issue?

ERROR 1064 (42000): 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 'conv(gpa DOUBLE)
RETURNS CHAR
BEGIN
DECLARE grade CHAR;
SELECT CASE WHEN gp' at line 1

Here is my code:

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 $

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-08-30 20:30:44

您在末尾处缺少一个分号:

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 因为这已经意味着 其他内容

You're missing a semi-colon near the end:

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;

Also, you probably shouldn't use the name conv as this is already means something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文