1

我有一个错误的 MySQL 函数。

CREATE FUNCTION getpolygon(lat DOUBLE, lon DOUBLE, radius SMALLINT, corner TINYINT)
  RETURNS geometry
    DETERMINISTIC
BEGIN  
DECLARE i INT; 
DECLARE a, b, c DOUBLE;  
DECLARE res TEXT;

IF corner < 3 || radius > 500 THEN  
    RETURN NULL;  
END IF;  

SET res = CONCAT(lat + radius / 111.12, ' ', lon, ',');  

WHILE i < corner  do
    SET c = RADIANS(360 / corner * i);  
    SET a = lat + COS(c) * radius / 111.12;  
    SET b = lon + SIN(c) * radius / (COS(RADIANS(lat + COS(c) * radius / 111.12 / 111.12)) * 111.12);  
    SET res = CONCAT(res, a, ' ', b, ',');  
    SET i = i + 1;  
END WHILE;  

RETURN GEOMFROMTEXT(CONCAT('POLYGON((', res, lat + radius / 111.12, ' ', lon, '))'));  
END;

当我运行此代码时,MySQL 服务器说:“SQL 错误 1064 ..... nerar '' at line 5”

问题是什么?

我感谢你的帮助!

4

1 回答 1

1
DELIMITER $$
    CREATE FUNCTION getpolygon(lat DOUBLE, lon DOUBLE, radius SMALLINT, corner TINYINT)
      RETURNS geometry
        DETERMINISTIC
    BEGIN
    DECLARE i TINYINT DEFAULT 1;  
    DECLARE a, b, c DOUBLE;  
    DECLARE res TEXT;

    IF corner < 3 || radius > 500 THEN  
        RETURN NULL;  
    END IF;  

    SET res = CONCAT(lat + radius / 111.12, '', lon, ',');  

    WHILE i < corner  do
        SET c = RADIANS(360 / corner * i);  
        SET a = lat + COS(c) * radius / 111.12;  
        SET b = lon + SIN(c) * radius / (COS(RADIANS(lat + COS(c) * radius / 111.12 / 111.12)) * 111.12);  
        SET res = CONCAT(res, a, '', b, ',');  
        SET i = i + 1;  
    END WHILE;  

    RETURN GEOMFROMTEXT(CONCAT('POLYGON((', res, lat + radius / 111.12, '', lon, '))'));  
    END$$
    DELIMITER ;
于 2015-08-24T06:48:16.900 回答