简体   繁体   中英

Syntax error while creating own function in MySQL

I tried to create own function in MySQL, but the code below doesn't work. There is any syntax error, but after 30mins I'm unable to find what is wrong. Any ideas?

DROP FUNCTION IF EXISTS `gps_distance`;;
CREATE FUNCTION `gps_distance`(lat1 FLOAT,lng1 FLOAT,lat2 FLOAT,lng2 FLOAT) RETURNS float
    READS SQL DATA
    DETERMINISTIC

BEGIN
SET lat1 = lat1 * pi() / 180;
SET lng1 = lng1 * pi() / 180;
SET lat2 = lat2 * pi() / 180;
SET lng2 = lng2 * pi() / 180;

    RETURN acos
    (   cos(lat1)*cos(lng1)*cos(lat2)*cos(lng2)
      + cos(lat1)*sin(lng1)*cos(lat2)*sin(lng2)
      + sin(lat1)*sin(lat2)
    ) * 6372.795;

END;;

The error is reported on line 6, after BEGIN clause.

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 6

Should it depends on MySQL version (I have 5.6.21)? On PMA version (4.3.0)? I tried to run this query from PHP script, the same problem.
Thanks for answers.

Are you sure that the delimiter is set to something other than semicolon

DROP FUNCTION IF EXISTS `gps_distance`;

DELIMITER $$

CREATE FUNCTION `gps_distance`(lat1 FLOAT,lng1 FLOAT,lat2 FLOAT,lng2 FLOAT) RETURNS float
READS SQL DATA
DETERMINISTIC

BEGIN
SET lat1 = lat1 * pi() / 180;
SET lng1 = lng1 * pi() / 180;
SET lat2 = lat2 * pi() / 180;
SET lng2 = lng2 * pi() / 180;

    RETURN acos
    (   cos(lat1)*cos(lng1)*cos(lat2)*cos(lng2)
      + cos(lat1)*sin(lng1)*cos(lat2)*sin(lng2)
      + sin(lat1)*sin(lat2)
    ) * 6372.795;

END $$

DELIMITER ;

add delimiter in procedure.

DELIMITER // 
DROP FUNCTION IF EXISTS `gps_distance`;
CREATE FUNCTION `gps_distance`(lat1 FLOAT,lng1 FLOAT,lat2 FLOAT,lng2 FLOAT) RETURNS float
    READS SQL DATA
    DETERMINISTIC

BEGIN
SET lat1 = lat1 * pi() / 180;
SET lng1 = lng1 * pi() / 180;
SET lat2 = lat2 * pi() / 180;
SET lng2 = lng2 * pi() / 180;

    RETURN acos
    (   cos(lat1)*cos(lng1)*cos(lat2)*cos(lng2)
      + cos(lat1)*sin(lng1)*cos(lat2)*sin(lng2)
      + sin(lat1)*sin(lat2)
    ) * 6372.795;

END //

DELIMITER;

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