Declare variable inside a function

software development

#1

I’ve got some simple code that I can’t seem to get to compile and I’m not sure if it’s a problem with my syntax or to do with the configuration of MySQL.

Basically, I’ve got a function that will return an integer. I’ve tried to declare the return variable as the first statement inside the BEGIN…END block of the function. But, it does not compile and I can’t work out why.

(I’ve simplified the query by substituting an integer for the subquery that I will be using once this code compiles.)

CREATE FUNCTION fn_GetYearAlbumRank(parmAlbumID INT, parmYear INT)
RETURNS INT
BEGIN
DECLARE retval INT;
SELECT 5 INTO retval;
RETURN retval;

END;

I get the following error (it doesn’t seem to like the declare statement):
#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 ‘INT’ at line 4