0
DELIMITER $$

    CREATE FUNCTION nameOfFunct(intIn int)
    RETURN int
    BEGIN
        DECLARE intOut INT;
        SET intOut = SELECT count(*)
            FROM tableToTakeFrom
            WHERE columToCompareTo = intIn;

        RETURN intOut;
    END;
        $$

DELIMITER;

If I try to run this all I get is:

SQL Error (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 'RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = select count(' at line 2

2 Answers 2

2

A couple of changes to resolve the problem:

DELIMITER $$

CREATE FUNCTION nameOfFunct(intIn INT)
-- RETURN INT
RETURNS INT
BEGIN
    DECLARE intOut INT;
    /*SET intOut = SELECT COUNT(*)
        FROM tableToTakeFrom
        WHERE columToCompareTo = intIn;*/
    SET intOut = (SELECT COUNT(*)
        FROM tableToTakeFrom
        WHERE columToCompareTo = intIn);
    RETURN intOut;
END $$

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

Comments

1

Defining the return type is done by the RETURNS keywrod, not the RETURN keyword:

CREATE FUNCTION nameOfFunct(intIn int)
RETURNS int
BEGIN
    DECLARE intOut INT;
    SET intOut = SELECT count(*) 
                 FROM   tableToTakeFrom
                 WHERE  columToCompareTo = intIn;
    RETURN intOut;
END;

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.