0

I need to select the first flight of today. For that I'm using

delimiter//
CREATE function get_flightID(flightident())
returns int
begin
return (SELECT * FROM flightdep where depday = dayofweek(CURDATE()) ORDER BY depTime asc LIMIT 1;)
end//
delimiter;

But this isn't working. When I execute this I got an error about MySQL syntax.

17:50:50 ) end// delimiter Error Code: 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 ') end// delimiter' at line 1 0.000 sec

I'm really guessing what to use in my first line, I tried everything I could think of.

CREATE function get_flightID(flightident())
2
  • 1
    Add the error message please. Commented Mar 8, 2015 at 16:38
  • 3
    Why is flightident() in your function definition? It is not used anywhere in the body of the function. Take that out. Commented Mar 8, 2015 at 16:38

2 Answers 2

1

First, you are returning select *, which would presumably have more than one column. Second, you have an unnecessary semicolon. Try something like this:

delimiter//
CREATE function get_flightID(flightident())
returns int
begin
    return (SELECT deptime
            FROM flightdep
            where depday = dayofweek(CURDATE())
            ORDER BY depTime asc
            LIMIT 1
           )
end//
delimiter;
Sign up to request clarification or add additional context in comments.

Comments

0

You're attempting to return a resultset (that is, SELECT *...) from your function, but you declared it to return an int.

You want something like this:

DELIMITER $$
DROP FUNCTION IF EXISTS get_flightID $$
CREATE function get_flightID()
    returns int
    reads sql data
    begin
        declare result int;

        SELECT flightID INTO result
          FROM flightdep 
         where depday = dayofweek(CURDATE())
         ORDER BY depTime asc LIMIT 1;

         return result;
    end$$
DELIMITER ;

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.