Home > Software design >  MySQL: SImple update query for stored procedure with nested If giving syntax error
MySQL: SImple update query for stored procedure with nested If giving syntax error

Time:05-04

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. 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

CodePudding user response:

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:

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; 

CodePudding user response:

I see several problems.

DELIMITER must set the delimiter on the same line. You cannot add a newline before the delimiter.

WRONG:

DELIMITER
 ;

RIGHT:

DELIMITER ;

Next, it does no good to set the DELIMITER to ; 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. I think you need to re-read https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html

Finally, the end of an IF compound statement is END IF;, not ENDIF;. See https://dev.mysql.com/doc/refman/8.0/en/if.html

  • Related