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 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'DECLARE v_dlat FLOAT; 附近使用的正确语法;声明 dlng 浮动;SET LatBegin = LatBegin * PI(' 在第 8 行

4

3 回答 3

2

你已经把整个函数写成一个大语句。您需要使用分隔符。这是MySQL 手册中的示例:

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)
于 2012-04-10T09:07:36.827 回答
0

;您必须以so结束每一行/命令

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

ETC .....

于 2012-04-10T09:06:18.160 回答
0

添加 ';' 正如 ManseUK 建议的那样。

所有声明必须在 BEGIN...END 子句的开头 -

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
于 2012-04-10T09:25:21.193 回答