mysqlcreate-function

MySQL Function use provided variables throws error


I want to create a function for calculating the distance between points.

The calculation is going as expected, but i receive an error here:

DROP FUNCTION IF EXISTS CalculateDistance;

CREATE FUNCTION CalculateDistance(breite double, laenge double) RETURNS INT READS SQL DATA

BEGIN

DECLARE breite DOUBLE;

SET @ibk_laenge = breite;

CREATE FUNCTION CalculateDistance(breite double, laenge double) RETURNS INT READS SQL DATA

BEGIN

DECLARE breite DOUBLE

MySQL meldet: Dokumentation 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 5

What's wrong with that?


Solution

  • You need to change the delimiter first:

    delimiter //
    CREATE FUNCTION CalculateDistance ...
    
    END //
    delimiter ;
    

    Otherwise the function definition stops at the first ; which would make it incomplete.