I have this Query which tries to update several values in another table after a insertion has been commited. Inside the trigger there is a function called calc_distance which returns a DOUBLE type value.
So, the problem is that no matter if I call the function properly the update is not taking the returned value. I tested the function running it separately from the mysql command prompt and in fact it returned the expected calculation.
So can someone please tell me what's going on on this query?
DELIMITER ;;
TRIGGER `DB1`.`update_coords`
AFTER INSERT ON `DB1`.`TABLE_tracking`
FOR EACH ROW
BEGIN
DECLARE LAT1, LAT2, LNG1, LNG2, DISTANCE DOUBLE;
UPDATE TABLE_vehiculos SET ultima_comunicacion = now(),
googlelat=(IF(NEW.latitude<>0,NEW.latitude,googlelat)), googlelong=(IF(NEW.longitude<>0,NEW.longitude,googlelong)),
tipo_data=NEW.command, reported_command=(IF(NEW.valid=1,"F","A")), velocidad=NEW.speed, status="A",
/* HERE COMES THE FUNCTION THAT IS NOT DOING ANYTHING */
odometro=odometro+calc_distance(googlelat,NEW.latitude,googlelong,NEW.lontitude)
/* --------------------------------------------------- */
WHERE imei = NEW.device_id;
END ;;
DELIMITER ;
I declared LAT1, LAT2, LNG1, LNG2, DISTANCE to set all those variables with NEW.latitude and so on, but I realized that I cannot do anything with those variables because I cannot access NEW.latitude and the values googlelat or googlelong until I call update tableX, so, what is going on?
I tested the calc_distance function like this:
mysql> select calc_distance(9.104550000000, 9.102577000000, -79.370780000000, -79.3622423000000);
+------------------------------------------------------------------------------------+
| calc_distance(9.104550000000, 9.102577000000, -79.370780000000, -79.3622423000000) |
+------------------------------------------------------------------------------------+
| 0.9627214584265752 |
+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Please I've been hitting my head against the monitor for the last 6 hours, please help!!
DELIMITER ;;
CREATE FUNCTION `calc_distance`(`lat1` DOUBLE, lat2 DOUBLE, lng1 DOUBLE, lng2 DOUBLE)
RETURNS DOUBLE
BEGIN
DECLARE radio INTEGER(5);
DECLARE distance DOUBLE;
set radio = 6371;
SELECT radio * 2 * ASIN
( SQRT (POWER(SIN((lat1 - lat2)*pi()/180 / 2),2) +
COS(lat1 * pi()/180) * COS(lat2 *pi()/180) *
POWER(SIN((lng1 - lng2) *pi()/180 / 2), 2) ) ) into distance;
RETURN distance;
END ;;
DELIMITER ;
googlelatabdgooglelong? Log their values from within your function.