创建mysql存储过程的第一行出错

时间:2013-12-14 15:48:24

标签: mysql stored-procedures

我正在尝试在MySQL社区服务器5.5.32版本中创建存储过程,但我的过程在第一行返回错误但我找不到它。任何人都可以帮助我吗?

CREATE PROCEDURE nearestPanel (IN mylat DOUBLE, IN mylon DOUBLE) 
BEGIN
    DECLARE dist DOUBLE DEFAULT 62.14;
    DECLARE lon1 DOUBLE DEFAULT 0;
    DECLARE lon2 DOUBLE DEFAULT 0;
    DECLARE lat1 DOUBLE DEFAULT 0;
    DECLARE lat2 DOUBLE DEFAULT 0;
    DECLARE today TIMESTAMP DEFAULT CURRENT_DATE;

    SET lon1 = mylon-dist/abs(cos(radians(mylat))*69);
    SET lon2 = mylon+dist/abs(cos(radians(mylat))*69);
    SET lat1 = mylat-(dist/69);
    SET lat2 = mylat+(dist/69);

SELECT 
    panels.name, 
    panels.description, 
    cells.*, 
    3956 * 2 * ASIN(SQRT(POWER(SIN((mylat - panels.latitude) * pi()/180 / 2), 2) +COS(mylat * pi()/180) * COS(panels.latitude * pi()/180) * POWER(SIN((mylon -panels.longitude) * pi()/180 / 2), 2))) AS distance
FROM 
    cells 
INNER JOIN 
    panels
ON 
    cells.panel_id = panels.id
WHERE 
    panels.longitude BETWEEN lon1 AND lon2 
AND 
    panels.latitude BETWEEN lat1 AND lat2 
AND 
    today BETWEEN panels.date_start AND panels.date_end
HAVING 
    distance < dist 
ORDER BY 
    distance LIMIT 1;
END

1 个答案:

答案 0 :(得分:2)

由于该过程包含分号,因此您需要暂时将分隔符从;更改为//;

DELIMITER //
CREATE PROCEDURE nearestPanel (IN mylat DOUBLE, IN mylon DOUBLE) 
BEGIN
    DECLARE dist DOUBLE DEFAULT 62.14;
    DECLARE lon1 DOUBLE DEFAULT 0;
...
    HAVING 
    distance < dist 
ORDER BY 
    distance LIMIT 1;
END//
DELIMITER ;

An SQLfiddle to test with, note the delimiter set to // in the dropdown below the code