如何“即时”返回带有存储过程中生成的值的附加列?

发布于 2024-08-06 13:28:03 字数 1244 浏览 4 评论 0原文

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

 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

有什么指点吗?包括问这个问题的正确方式?

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

I have a stored procedure in SQL 2005 that calculates distance using the Haversine formula. Everything works quite nicely but I'd like to return the calculated distance with my result set. How do I go about adding that column/value pair?

 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

Any pointers? Including the correct way to ask this question?

(Oh, and yes, this is not the stored procedure since I was playing with the query directly that's what I pasted in here.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-08-13 13:28:03

我想这就是你所要求的

    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)

I think this is what your are asking for

    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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文