0

I'm using phpmyadmin and MySQL to run a simple query, that creates a function checking the existence of a certain record. It keeps throwing a syntax error at line 7 with Declare. I have no idea why. I did try to use the built-in function creator, but it's messed up and I don't like it. Any help appreciated!

CREATE FUNCTION  check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA

    BEGIN
    DECLARE res INT; --line 7
    IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
    THEN SET res = 1;
    ELSE SET res = 0;
    END IF;
    RETURN res;
    END
0

1 Answer 1

1

Try the following:

DELIMITER $$
DROP FUNCTION IF EXISTS `check_if_card_exists`$$

CREATE FUNCTION  check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA

    BEGIN
    DECLARE res INT; --line 7
    IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
    THEN SET res = 1;
    ELSE SET res = 0;
    END IF;
    RETURN res;
    END$$

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

Comments

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.