Nick
Nick

Reputation: 16105

create function mysql error

Here is my function

DELIMITER //
CREATE FUNCTION specialUser(user INT) RETURNS user_id INT
BEGIN
    DECLARE user_id INT DEFAULT 0;
    CASE 
     WHEN user = 1556 THEN SET user_id = 1001;
     WHEN user = 1018 THEN SET user_id = 1002;
     WHEN user = 3658 THEN SET user_id = 1003;
         ELSE SET user_id = user;
    END CASE;
 RETURN user_id;
END//

I get error:

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 'user_id INT BEGIN DECLARE user_id INT DEFAULT 0; CASE ' at line 1

Upvotes: 1

Views: 1020

Answers (1)

AndreKR
AndreKR

Reputation: 33707

You can only state the type of the result, there's no use in giving an identifier, so:

CREATE FUNCTION specialUser(user INT) RETURNS INT

instead of

CREATE FUNCTION specialUser(user INT) RETURNS user_id INT

Upvotes: 3

Related Questions