     JOIN postal_code_longitude pclo 
       ON pclo.postal_code_id = pcla.postal_code_id 
         AND pclo.country_id = pcla.country_id
-    WHERE pclo.postal_code_id = postal_code_2_id 
+    WHERE pclo.postal_code_id = the_postal_code_id_2 
       AND pclo.country_id = the_country_id_2;
 
     SET lat_first_rad = dec_deg_to_rad(lat_first);

CREATE FUNCTION calc_approx_dist
(
  the_country_id_1 INT,
  pcode1 VARCHAR(30),
  the_country_id_2 INT,
  pcode2 VARCHAR(30)
)
RETURNS DOUBLE DETERMINISTIC READS SQL DATA
BEGIN

    DECLARE delta_long DOUBLE;
    DECLARE earth_radius DOUBLE;
    DECLARE distance DOUBLE;

    IF the_country_id_1 IS NULL OR 
      the_country_id_2 IS NULL OR 
      the_postal_code_id_1 IS NULL OR 
      the_postal_code_id_2 IS NULL
      THEN RETURN NULL;
    END IF;

    SELECT pcla.latitude, pclo.longitude INTO lat_first, long_first
    FROM postal_code_latitude pcla
    JOIN postal_code_longitude pclo 
      ON pclo.postal_code_id = pcla.postal_code_id 
        AND pclo.country_id = pcla.country_id
    WHERE pclo.postal_code_id = the_postal_code_id_1 
      AND pclo.country_id = the_country_id_1;

    SELECT pcla.latitude, pclo.longitude INTO lat_second, long_second
