Varun Jain
Varun Jain

Reputation: 1911

Stored function with multiple parameters and return type

I am trying to implement the following stored procedure but it's not getting created :

  DELIMITER  //
CREATE Function 'geodistkm'   
(lat1  FLOAT ,  lon1  FLOAT,  lat2 FLOAT ,  lon2 FLOAT) 
returns FLOAT
BEGIN
 DECLARE pi, q1, q2, q3 FLOAT;
 DECLARE rads FLOAT DEFAULT 0;
 SET pi = PI();
 SET lat1 = lat1 * pi / 180;
 SET lon1 = lon1 * pi / 180;
 SET lat2 = lat2 * pi / 180;
 SET lon2 = lon2 * pi / 180;
 SET q1 = COS(lon1-lon2);
 SET q2 = COS(lat1-lat2);
 SET q3 = COS(lat1+lat2);
 SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) ); 
 RETURN 6378.388 * rads;
END ;
//

DELIMITER ;

I can't seem to figure out what's wrong with it .

Upvotes: 0

Views: 9762

Answers (1)

Varun Jain
Varun Jain

Reputation: 1911

Worked with the following changes :

DELIMITER  //
CREATE FUNCTION  geodistkm  
(lat1  FLOAT ,  lon1  FLOAT,  lat2 FLOAT ,  lon2 FLOAT) 
RETURNS  FLOAT
BEGIN
 DECLARE pi, q1, q2, q3 FLOAT;
 DECLARE rads FLOAT DEFAULT 0;
 SET pi = PI();
 SET lat1 = lat1 * pi / 180;
 SET lon1 = lon1 * pi / 180;
 SET lat2 = lat2 * pi / 180;
 SET lon2 = lon2 * pi / 180;
 SET q1 = COS(lon1-lon2);
 SET q2 = COS(lat1-lat2);
 SET q3 = COS(lat1+lat2);
 SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) ); 
 RETURN 6378.388 * rads;
END //


DELIMITER ;

Upvotes: 1

Related Questions