如何使用存储过程中生成的值返回附加列“飞行”?

时间:2009-10-01 15:59:40

标签: sql-server-2005 haversine

我在SQL 2005中有一个存储过程,它使用Haversine公式计算距离。一切都很好,但我想用结果集返回计算的距离。如何添加该列/值对?

 DECLARE @Longitude DECIMAL(18,8),
        @Latitude DECIMAL(18,8),
        @MinLongitude DECIMAL(18,8),
        @MaxLongitude DECIMAL(18,8),
        @MinLatitude DECIMAL(18,8),
        @MaxLatitude DECIMAL(18,8),
        @WithinMiles INT

Set @Latitude = -122.25336930
Set @Longitude = 37.50002600
Set @WithinMiles = 20

-- Calculate the Max Lat/Long
SELECT @MaxLongitude = dbo.LongitudePlusDistance(@Longitude, @Latitude, @WithinMiles),
       @MaxLatitude = dbo.LatitudePlusDistance(@Latitude, @WithinMiles)

-- Calculate the min lat/long
SELECT @MinLatitude = 2 * @Latitude - @MaxLatitude,
       @MinLongitude = 2 * @Longitude - @MaxLongitude

SELECT Top 10 *
FROM   Location
WHERE  LocationLongitude Between @MinLongitude And @MaxLongitude
       And LocationLatitude Between @MinLatitude And @MaxLatitude
       And dbo.CalculateDistance(@Longitude, @Latitude, LocationLongitude, LocationLatitude) <= @WithinMiles
ORDER BY dbo.CalculateDistance(@Longitude, @Latitude, LocationLongitude, LocationLatitude)

--Return the result of dbo.CalculateDistance

任何指针?包括问这个问题的正确方法?

(哦,是的,这不是存储过程,因为我直接使用了查询,这就是我在这里粘贴的内容。)

1 个答案:

答案 0 :(得分:1)

我认为这就是你的要求

    SELECT Top 10 *,dbo.CalculateDistance(@Longitude, @Latitude, LocationLongitude, LocationLatitude) as 'Calculated Distance'
FROM   Location
WHERE  LocationLongitude Between @MinLongitude And @MaxLongitude
       And LocationLatitude Between @MinLatitude And @MaxLatitude
       And dbo.CalculateDistance(@Longitude, @Latitude, LocationLongitude, LocationLatitude) <= @WithinMiles
ORDER BY dbo.CalculateDistance(@Longitude, @Latitude, LocationLongitude, LocationLatitude)