我试图在MySQL 5.5.38中编写我的第一个存储函数,但我无法通过一个恼人的语法错误。我已经检查了MySQL文档,但我仍然无法看到问题是什么,错误信息只显示You have an error in your SQL syntax; blah blah blah ... at line 1
。
以下是代码:
DELIMITER $$
CREATE FUNCTION distm (lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2 DOUBLE)
RETURNS DOUBLE NO SQL DETERMINISTIC
BEGIN
DECLARE radius DOUBLE;
SET radius = 6371008.771415059;
DECLARE x1, y1, x2, y2, dlat, dlng DOUBLE;
SET x1 = RADIANS(lng1);
SET y1 = RADIANS(lat1);
SET x2 = RADIANS(lng2);
SET y2 = RADIANS(lat2);
SET dlng = x2 - x1;
SET dlat = y2 - y1;
DECLARE dist DOUBLE;
SET dist = 2 * radius * ASIN(
SQRT(
POW(SIN(dlat / 2), 2)
+ COS(y1) * COS(y2) * POW(SIN(dlng / 2), 2)
)
);
RETURN dist;
END$$
DELIMITER ;
编辑:确切的错误消息如下:
SQL Error [1064] [42000]: 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 'DELIMITER $$
CREATE FUNCTION distm (lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2' at line 1
答案 0 :(得分:0)
尝试删除DROP
之前的DELIMITER $$
语句,然后重新运行。
另外,将行RETURNS DOUBLE NO SQL DETERMINISTIC
更改为以下。请注意,它不是NO SQL DETERMINISTIC
而是NOT DETERMINISTIC
RETURNS DOUBLE NOT DETERMINISTIC