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

    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 
      AND pclo.country_id = the_country_id_2;

    SET lat_first_rad = dec_deg_to_rad(lat_first);

    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
