USE [Test_DB] GO /****** Object: UserDefinedFunction [dbo].[fnGetDistance] Script Date: 08/17/2017 16:13:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[fnGetDistance] ( @GPSLng DECIMAL(12,6), //起始经度 @GPSLat DECIMAL(12,6), //起始纬度 @Lng DECIMAL(12,6), @Lat DECIMAL(12,6) ) RETURNS DECIMAL(12,4) AS BEGIN DECLARE @result DECIMAL(12,4) SELECT @result = 6378137.0*ACOS(SIN(@GPSLat/180*PI())*SIN(@Lat/180*PI())+COS(@GPSLat/180*PI())*COS(@Lat/180*PI())*COS((@GPSLng-@Lng)/180*PI())) RETURN @result END