doubu1970 2015-03-27 14:56
浏览 150
已采纳

在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.

  • 写回答

2条回答 默认 最新

  • duanfangbunao36970 2015-03-27 15:08
    关注

    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 ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题