Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
where is the DELIMITER ? –  MySQLRockstar 21 hours ago
    
You didn't change the delimiter, so your ; are terminating the function definition, before it ever really gets started. –  Marc B 21 hours ago

2 Answers 2

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 ;
share|improve this answer

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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.