Mysql CREATE FUNCTION语法错误

时间:2012-04-10 09:00:05

标签: mysql

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附近使用正确的语法;             DECLARE dlng FLOAT;         SET LatBegin = LatBegin * PI('第8行

3 个答案:

答案 0 :(得分:1)

你把整个函数写成一个大的陈述。您需要使用分隔符。以下是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)

答案 1 :(得分:0)

您必须使用;

结束每一行/命令
BEGIN
    DECLARE Distance FLOAT;
    DECLARE EARTH_RADIUS FLOAT;
    SET EARTH_RADIUS =  6378137.00;

等......

答案 2 :(得分:0)

添加';'正如马苏克建议的那样。

所有声明必须位于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