简体   繁体   English

MySQL:使用嵌套 If 给出语法错误的存储过程的简单更新查询

[英]MySQL: SImple update query for stored procedure with nested If giving syntax error

I am creating a SQL query to create a stored procedure to update the columns Leaders_Score & Leaders_Icon of a table, say chicago_public_schools.我正在创建一个 SQL 查询来创建一个存储过程来更新表的 Leaders_Score 和 Leaders_Icon 列,比如 chicago_public_schools。 I have checked the syntax again & again but could not figure out why I get syntax error, can anyone please advise what I am doing wrong in this code:我已经一次又一次地检查了语法,但无法弄清楚为什么会出现语法错误,任何人都可以告诉我这段代码中做错了什么:

   DELIMITER
    ;
CREATE PROCEDURE UPDATELEADERSSCORE(
    IN inSchoolID INTEGER,
    IN inLeaderScore INTEGER
) LANGUAGE SQL
BEGIN
    UPDATE
        chicago_public_schools
    SET
        Leaders_Score = inLeaderScore
    WHERE
        School_ID = inSchoolID; IF inLeaderScore > 80 AND inLeaderScore < 99 THEN
    UPDATE
        chicago_public_schools
    SET
        Leaders_Icon = 'Very Strong'
    WHERE
        School_ID = inSchoolID; ELSEIF inLeaderScore > 60 AND inLeaderScore < 79 THEN
    UPDATE
        chicago_public_schools
    SET
        Leaders_Icon = 'Strong'
    WHERE
        School_ID = inSchoolID; ELSEIF inLeaderScore > 40 AND inLeaderScore < 59 THEN
    UPDATE
        chicago_public_schools
    SET
        Leaders_Icon = 'Average'
    WHERE
        School_ID = inSchoolID; ELSEIF inLeaderScore > 20 AND inLeaderScore < 39 THEN
    UPDATE
        chicago_public_schools
    SET
        Leaders_Icon = 'Weak'
    WHERE
        School_ID = inSchoolID; ELSEIF inLeaderScore > 0 AND inLeaderScore < 19 THEN
    UPDATE
        chicago_public_schools
    SET
        Leaders_Icon = 'Very weak'
    WHERE
        School_ID = inSchoolID; ENDIF;
END;
DELIMITER
    ;

Here is the error:这是错误:

CREATE PROCEDURE UPDATELEADERSSCORE(
    IN inSchoolID INTEGER,
    IN inLeaderScore INTEGER
) LANGUAGE SQL
BEGIN
    UPDATE
        chicago_public_schools
    SET
        Leaders_Score = inLeaderScore
    WHERE
        School_ID = inSchoolID
MySQL said: Documentation

#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 11

Regards, T问候,T

It looks like you're trying to set the value, and then a category based on the value.看起来您正在尝试设置值,然后是基于该值的类别。 It would be a lot cleaner with a CASE statement like:使用 CASE 语句会更清晰,例如:

UPDATE chicago_public_schools
SET
    Leaders_Score = inLeaderScore,
    Leaders_Icon = CASE WHEN inLeaderScore > 80 THEN 'Very Strong'
                        WHEN inLeaderScore > 60 THEN 'Strong'
                        WHEN inLeaderScore > 40 THEN 'Average'
                        WHEN inLeaderScore > 20 THEN 'Weak'
                        WHEN inLeaderScore > 0 THEN 'Very Weak'
                    END
WHERE
    School_ID = inSchoolID; 

I see several problems.我看到几个问题。

DELIMITER must set the delimiter on the same line. DELIMITER必须在同一行设置分隔符。 You cannot add a newline before the delimiter.您不能在分隔符前添加换行符。

WRONG:错误的:

DELIMITER
 ;

RIGHT:正确的:

DELIMITER ;

Next, it does no good to set the DELIMITER to ;接下来,将 DELIMITER 设置为; before you write a stored routine which contains ;在编写包含的存储例程之前; statement terminators in the body of the routine.例程主体中的语句终止符。 You need to define the delimiter to something different than the default statement terminator, so a semicolon in the routine body doesn't terminate the whole CREATE PROCEDURE statement.您需要将定界符定义为不同于默认语句终止符的内容,因此例程主体中的分号不会终止整个 CREATE PROCEDURE 语句。 I think you need to re-read https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html我认为您需要重新阅读https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html

Finally, the end of an IF compound statement is END IF;最后, IF复合语句的结尾是END IF; , not ENDIF; , 不是ENDIF; . . See https://dev.mysql.com/doc/refman/8.0/en/if.html参见https://dev.mysql.com/doc/refman/8.0/en/if.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM