• Sounds good, however, you could comment out the following line and everything will still work:

    IF ((@lat1 = @lat2) AND (@lon1 = @lon2)) RETURN 0

    Also, see the wiki article regarding earth radius at:

    https://en.wikipedia.org/wiki/Great-circle_distance

    A good choice for the radius is the mean earth radius, ... 6371.0 km (for the WGS84 ellipsoid); in the limit of small flattening, this choice minimizes the mean square relative error in the estimates for distance.

    Not sure about your application needs, but the value above might help reduce errors in distance calculations.