按计算距离(从坐标)过滤结果

时间:2017-09-15 16:34:25

标签: sql-server tsql

我试图通过仅返回半径(以公里为单位)的“事件”来过滤表中的结果但​​是我的存储过程和函数的组合不起作用...它没有返回结果..如果我改变我的函数中的返回值,例如计算距离为5 Km并运行半径更大的存储过程然后返回5 Km返回结果..所以也许我的函数错了?

这是功能

ALTER FUNCTION [dbo].[fnCalcDistanceKM](@lat1 float, @lat2 float, @lon1 
float, @lon2 float)
RETURNS FLOAT 
AS
BEGIN
DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::Point(@lat1, @lon1, 4326);
SET @h = geography::Point(@lat2, @lon2, 4326);
return @g.STDistance(@h);
END

这是SP

CREATE PROCEDURE [dbo].[GetEventByDistance]
@Latitude float,   
@Longitude float,  
@Radius int
AS   
with MyCte as
(
    select dbo.fnCalcDistanceKM(l.Latitude, l.Longitude, @Latitude, @Longitude) as distance, a.*, l.Adress, l.City, l.Latitude, l.Longitude, l.PSC
    from dbo.Events a 
    JOIN Locations as l ON l.Id = a.LocationId
)
SELECT MyCte.Adress, MyCte.City, MyCte.Description, MyCte.Doporuceno, MyCte.EndDate, MyCte.Id, MyCte.Latitude, MyCte.LocationId, MyCte.Longitude, MyCte.Name, MyCte.ProvozovatelId, MyCte.PSC, MyCte.StartDate, MyCte.TypAkce
From MyCte
where distance < @Radius
RETURN 
GO

0 个答案:

没有答案