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

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

图片转代码服务由CSDN问答提供 功能建议

我试图在MySQL中创建自己的函数,但下面的代码不起作用。 有任何语法错误,但30分钟后我无法找到错误。 任何想法?

  DROP FUNCTION如果存在`gps_distance` ;; 
创建函数`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 ;; 
   
 
 

错误 在 BEGIN 子句之后的第6行报告。

您的SQL语法中有错误; 查看与MySQL服务器版本对应的手册,以便在第6行附近使用正确的语法

它是否取决于MySQL版本(我有5.6。 21)? 在PMA版本(4.3.0)? 我试图从PHP脚本运行此查询,同样的问题。
谢谢你的答案。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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 ;
    
    已采纳该答案
    打赏 评论
  • dongyao5843 2015-03-27 15:09

    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;
    
    打赏 评论

相关推荐 更多相似问题