sqlmysqldatabasefunctionmysql-function

MySQL CREATE FUNCTION Syntax


I am trying to create a function in MySQL:

Here is the SQL code:

CREATE FUNCTION F_Dist3D (x1 decimal, y1 decimal) 
RETURNS decimal
DETERMINISTIC
BEGIN 
  DECLARE dist decimal;
  SET dist = SQRT(x1 - y1);
  RETURN dist;
END;

I am getting the following 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 '' at line 10

I am running this create statement in phpMyAdmin. What is wrong with this function?


Solution

  • You have to override your ; delimiter with something like $$ to avoid this kind of error.

    After your function definition, you can set the delimiter back to ;.

    This should work:

    DELIMITER $$
    CREATE FUNCTION F_Dist3D (x1 decimal, y1 decimal) 
    RETURNS decimal
    DETERMINISTIC
    BEGIN 
      DECLARE dist decimal;
      SET dist = SQRT(x1 - y1);
      RETURN dist;
    END$$
    DELIMITER ;