0

I am unable to debug the code for CREATE FUNCTION. It gives the following error:

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 '' at line 16

This is the code.

DELIMITER $$
CREATE FUNCTION CHECK_STATUS(SAL FLOAT) RETURNS VARCHAR(10)
DETERMINISTIC
BEGIN
    DECLARE STATUS VARCHAR(10);
    SET STATUS = "";
    IF (SAL > 70000) THEN
    SET STATUS = "A CLASS";
    ELSEIF (SAL > 50000) THEN
    SET STATUS = "B CLASS";
    ELSE IF (SAL > 30000) THEN 
    SET STATUS = "C CLASS";
    ELSE
    SET STATUS = "D CLASS";
    END IF;
RETURN STATUS; # This is line 16
END; 
$$

PS: I have seen other question with same title but they were about the DELIMITER so they didn't help. My MySQL version is 5.7.19

1
  • Well from what I have read it might be that your last two lines should become END$$ and then a new line to reset the DELIMITER back to ; DELIMITER ; - This is not tested. Commented Oct 29, 2017 at 12:57

1 Answer 1

2

It will work if you change ELSE IF to ELSEIF. It is treated as ELSE and then unclosed IF clause, without END IF.

Sign up to request clarification or add additional context in comments.

3 Comments

Just saw that there is one of each of the above in the OP's example.
Ok that worked. But then why It said error in line 16, why not error near ELSE IF something like that. @Michael O.
@NikhilWagh Because you've had an unclosed IF clause (part of ELSE IF) till the end of the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.