0
CREATE DEFINER = CURRENT_USER FUNCTION GET_DISTANCE (LatBegin float,LngBegin float,LatEnd float,LngEnd float)
    RETURNS float
    BEGIN
        DECLARE Distance FLOAT
    DECLARE EARTH_RADIUS FLOAT
    SET EARTH_RADIUS =  6378137.00

    DECLARE dlat FLOAT
            DECLARE dlng FLOAT
    SET LatBegin =  LatBegin * PI()/ 180.0
    SET LngBegin = LngBegin * PI() / 180.0
        SET LatEnd = LatEnd * PI() / 180.0
    SET LngEnd = LngEnd * PI() / 180.0

    SET dlat =  LatBegin - LatEnd
    SET dlng = LngBegin - LngEnd

    SET Distance = (1-cos(dlat))/2.0+cos(LatBegin)*cos(LatEnd)*((1-cos(dlng))/2.0)
    SET Distance = asin(sqrt(Distance)) * EARTH_RADIUS *2.0
    SET Distance = Round(Distance * 10000,2) / 10000
    RETURN Distance
    END

[Err] 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 'DECLARE v_dlat FLOAT; DECLARE dlng FLOAT; SET LatBegin = LatBegin * PI(' at line 8

3 Answers 3

2

You've written the whole function as one big statement. You need to use delimiters. Here's the example from the MySQL manual:

mysql> delimiter //

mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
    -> BEGIN
    ->   SELECT COUNT(*) INTO param1 FROM t;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)
Sign up to request clarification or add additional context in comments.

Comments

0

You must end each line/command with a ; so

BEGIN
    DECLARE Distance FLOAT;
    DECLARE EARTH_RADIUS FLOAT;
    SET EARTH_RADIUS =  6378137.00;

etc .....

Comments

0

Add ';' as ManseUK suggested.

All declarations must be at the begining of the BEGIN...END clause -

CREATE DEFINER = CURRENT_USER FUNCTION GET_DISTANCE(LatBegin FLOAT,
                                                    LngBegin FLOAT,
                                                    LatEnd   FLOAT,
                                                    LngEnd   FLOAT
                                                    )
RETURNS FLOAT
BEGIN
  DECLARE Distance     FLOAT;
  DECLARE EARTH_RADIUS FLOAT;
  DECLARE dlat         FLOAT;
  DECLARE dlng         FLOAT;

  SET EARTH_RADIUS = 6378137.00;
  SET LatBegin = LatBegin * PI() / 180.0;
  SET LngBegin = LngBegin * PI() / 180.0;
  SET LatEnd = LatEnd * PI() / 180.0;
  SET LngEnd = LngEnd * PI() / 180.0;
  SET dlat = LatBegin - LatEnd;
  SET dlng = LngBegin - LngEnd;

  SET Distance = (1 - COS(dlat)) / 2.0 + COS(LatBegin) * COS(LatEnd) * ((1 - COS(dlng)) / 2.0);
  SET Distance = ASIN(SQRT(Distance)) * EARTH_RADIUS * 2.0;
  SET Distance = ROUND(Distance * 10000, 2) / 10000;
  RETURN Distance;
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.