0

mysqlfile.sql:

DROP FUNCTION IF EXISTS func1;
DELIMITER //
CREATE FUNCTION func1(N INT) RETURNS INT
BEGIN
  RETURN (
      select * from Employee;
  );
END//
DELIMITER ;

executed source mysqlfile.sql, got an error:

ERROR 1064 (42000): 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' at line 4

How should I modify the code?

1
  • 1
    Functions can not return data sets. Commented Aug 18, 2015 at 8:44

4 Answers 4

1

A function returns 1 value, as far I can see you want to return a dataset. You must create procedure in order to get what you need.

 DELIMITER //
 CREATE PROCEDURE getEmployees()
 BEGIN
  SELECT *  FROM employee;
 END //
 DELIMITER ;

You can call it by call getEmployees();

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

4 Comments

Thank you, it worked great by deleting the semicolon at the end of the select code. And I just tested, function worked very well in this case if Employee only contained one column. There's no need to change it to a procedure.
yes but you needed all the columns as far I can your query..did you test it with multiple rows?
Yeah, you're right, multiple rows doesn't work with function. :)
good :) I went through the details in order to make it work..Learned as well
1

you missed a semicolon after END and have an extra after the select

DROP FUNCTION IF EXISTS func1;
DELIMITER //
CREATE FUNCTION func1(N INT) RETURNS INT
BEGIN
  RETURN (
      select * from Employee
  );
END;//
DELIMITER ;

3 Comments

Thank you. I just tested, it worked well without the semicolon after END
@MarkZar you are right! for me it works both ways, wouldn't have expected that the semicolon after END would be optional, I have been using it all this time :/.
@MarkZar I just checked my DB though, and it seems that both with or without produce the same create code when dumping the function, a rewritten version without the semicolon, so I suppose without it is the proper way.
0

You have missed 'AS' before BEGIN

Comments

0

TRY:

DROP FUNCTION IF EXISTS func1;
CREATE FUNCTION func1(N INT) RETURNS INT
BEGIN
DECLARE select_var VARCHAR;
SET select_var = (SELECT * FROM Employee);
RETURN VARCHAR(255);
END$$

Hope it'll solve your issue

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.