Question

I have the following 'haversin' algorithm implemented as an SQL function.

DELIMITER //
CREATE FUNCTION `db`.`haversin` (slat FLOAT, slon FLOAT, dlat FLOAT, dlon FLOAT) 
RETURNS FLOAT
BEGIN
 declare alphaLon,a FLOAT;
 set alphaLon=(slon–dlon);
 set a=3956*2*ASIN(SQRT( POWER(SIN((slat - abs(dlat)) * pi()/180 / 2),2) + COS(slat * pi()/180 ) * COS(abs(dlat) *  pi()/180) * POWER(SIN((alphaLon) *  pi()/180 / 2), 2) ));
 return a;
 END 
//delimiter;

when executing the above, i get a 1064 error 'syntax error near //delimiter', however, the function DOES get created.

what's confusing the most, when running the function, thats the result:

mysql> SELECT haversin(2.0,45.0,2.0,1.2);
ERROR 1054 (42S22): Unknown column 'slon–dlon' in 'field list'

how is this even possible?

Was it helpful?

Solution

  1. The code you have pasted contains three instructions to your MySQL client:

    1. Change its statement delimiter to //;

    2. Send to the server a CREATE FUNCTION command; and

    3. An unknown instruction delimiter;.

    Thus the first two commands succeed and the third fails. To rectify the failure of the third command, insert whitespace between the delimiter keyword and the desired new delimiter.

  2. Stored programs are not evaluated until they are executed: thus runtime errors (such as "unknown column") will not be detected until the function is invoked. Indeed, this makes perfect sense given that the database schema could change after a function is defined.

    You are receiving this error because your minus character is in fact an EN DASH (U+2013). For arithmetic subtraction, replace with HYPHEN-MINUS (U+002D).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top