经验 16 分贝 0 家园分 49 在线时间: 5 小时 最后登录: 2017-4-5 帖子: 9 精华: 0 注册时间: 2010-10-29 UID: 588816
注册:2010-10-29
发表于 2016-2-22 11:04:07
| 显示全部楼层
ALTER FUNCTION fnGetDistance7(@lon1 decimal(28, 15), @lat1 decimal(28, 15), @lon2 decimal(28, 15), @lat2 decimal(28, 15)) RETURNS decimal(28, 15)
AS
BEGIN
--距离(千米)
DECLARE @a_2d decimal(28, 15),@e_2d decimal(28, 15),@h_2d Integer,@DEG_2_RAD decimal(28, 15),@RAD_2_DEG decimal(28, 15)
DECLARE @x_rads decimal(28, 15),@y_rads decimal(28, 15),@n_2ds decimal(28, 15),@x_2d decimal(28, 15),@y_2d decimal(28, 15),@z_2d decimal(28, 15),@x_radm decimal(28, 15),@y_radm decimal(28, 15),@n_2dm decimal(28, 15),@x_2d_mark decimal(28, 15),@y_2d_mark decimal(28, 15),@z_2d_mark decimal(28, 15)
DECLARE @curdistance decimal(28, 15),@Distance decimal(28, 15)
--DECLARE @ALL varchar(255)
SET @a_2d = 6378.137
SET @e_2d = 0.00669438
SET @h_2d = 15
SET @DEG_2_RAD = 0.01745329252
SET @RAD_2_DEG = 57.2957795129
SET @x_rads = Abs(@lon1) * @DEG_2_RAD
SET @y_rads = Abs(@lat1) * @DEG_2_RAD
SET @n_2ds = @a_2d / Sqrt(1 - @e_2d * Sin(@y_rads) * Sin(@y_rads))
SET @x_2d = (@n_2ds + @h_2d) * Cos(@y_rads) * Cos(@x_rads)
SET @y_2d = (@n_2ds + @h_2d) * Cos(@y_rads) * Sin(@x_rads)
SET @z_2d = (@n_2ds * (1 - @e_2d) + @h_2d) * Sin(@y_rads)
SET @x_radm = Abs(@lon2) * @DEG_2_RAD
SET @y_radm = Abs(@lat2) * @DEG_2_RAD
SET @n_2dm = @a_2d / Sqrt(1 - @e_2d * Sin(@y_radm) * Sin(@y_radm))
SET @x_2d_mark = (@n_2dm + @h_2d) * Cos(@y_radm) * Cos(@x_radm)
SET @y_2d_mark = (@n_2dm + @h_2d) * Cos(@y_radm) * Sin(@x_radm)
SET @z_2d_mark = (@n_2dm * (1 - @e_2d) + @h_2d) * Sin(@y_radm)
SET @curdistance = (@x_2d_mark - @x_2d) * (@x_2d_mark - @x_2d) + (@y_2d_mark - @y_2d) * (@y_2d_mark - @y_2d) + (@z_2d_mark - @z_2d) * (@z_2d_mark - @z_2d)
SET @Distance = Sqrt(@curdistance)
--SET @ALL=CONVERT(varchar(255),@x_rads ) +'_'+CONVERT(varchar(255),@y_rads )+'_'+CONVERT(varchar(255),@n_2ds )+'_'+CONVERT(varchar(255),@x_2d )+'_'+CONVERT(varchar(255),@y_2d )+'_'+CONVERT(varchar(255),@z_2d )+'_'+CONVERT(varchar(255),@x_radm )+'_'+CONVERT(varchar(255),@y_radm )+'_'+CONVERT(varchar(255),@n_2dm )+'_'+CONVERT(varchar(255),@x_2d_mark )+'_'+CONVERT(varchar(255),@y_2d_mark )+'_'+CONVERT(varchar(255),@z_2d_mark )+'_'+CONVERT(varchar(255),@curdistance )+'_'+CONVERT(varchar(255),@Distance )
RETURN @Distance
END
--------------------------------
使用举例
select *,dbo.fnGetDistance7([经度], [纬度],[lon], [lat]) as ds from [ncgsm].[dbo].[4G工参],[ncgsm].[dbo].[2G工参] where dbo.fnGetDistance7([经度], [纬度],[lon], [lat])<2