简体   繁体   中英

Syntax error when creating mysql function

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?

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 ;

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();

您在开始之前错过了“ AS”

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM