0

So, below is the function i'm writing(in MySQL) and I get a syntax error at the last "RETURN" line (RETURN d_count). I'm sure it's some simple thing, but I can't figure it out. Thanks!

DELIMITER $$
CREATE FUNCTION dept_count (dept_name VARCHAR(20))
    RETURNS INT DETERMINISTIC
    BEGIN
    DECLARE d_count INT;
    SELECT COUNT(*) into d_count
    FROM instructor
    WHERE instructor.dept_name=dept_name
RETURN d_count;
END $$
2
  • 3
    You need a semicolon betweeh the query and RETURN. Commented Feb 13, 2015 at 23:53
  • EVERYTIME! thanks @Barmar you ROCK! Commented Feb 13, 2015 at 23:55

1 Answer 1

2

You should separate SELECT and RETURN with semicolon ; :

DELIMITER $$
CREATE FUNCTION dept_count (dept_name VARCHAR(20))
    RETURNS INT DETERMINISTIC
    BEGIN
    DECLARE d_count INT;
    SELECT COUNT(*) into d_count
    FROM instructor
    WHERE instructor.dept_name=dept_name;
    RETURN d_count;
END $$
Sign up to request clarification or add additional context in comments.

3 Comments

so, SELECT count(*) into d_count; and return d_count;
WHERE instructor.dept_name=dept_name;, he marked it for you. You don't put semicolons in between a statement such as SELECT...FROM...WHERE.
i see it now, i thought he was taking about a different spot. sorry, very new to sql, and i thought it would be odd to put a semi colon in that spot.

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.