I have come up with a practical use for MySQL Stored Procedures and developed a very useful example for the sceptics. The following is a MySQL SP that calculates the distance between two ZIP codes - all you pass it is the zip codes! This code assumes you have a database of US ZIP Codes with their Longitudes and Latitudes.
MySQL CODE DELIMITER // CREATE PROCEDURE `zipDist`(zipA INT, zipB INT) BEGIN DECLARE latA DECIMAL(10,6); DECLARE lonA DECIMAL(10,6); DECLARE latB DECIMAL(10,6); DECLARE lonB DECIMAL(10,6); SELECT latitude, longitude INTO latA, lonA FROM zipcodes WHERE zip=zipA; SELECT latitude, longitude INTO latB, lonB FROM zipcodes WHERE zip=zipB; SELECT ACOS(SIN(RADIANS(latA)) * SIN(RADIANS(latB)) + COS(RADIANS(latA)) * COS(RADIANS(latB)) * COS(RADIANS(lonB) - RADIANS(lonA))) * 3956 AS distance; END// DELIMITER ; Using this stored procedure in your code will save you 2 MySQL queries and a little bit of math in your code. »