sql函数与最近的顺序之间的距离

时间:2011-01-10 23:41:59

标签: sql sql-server nerddinner

我正在使用此功能来获取30英里范围内的事件,它与书呆子晚餐相似。但是我如何通过'最近'来订购它们??

ALTER FUNCTION [dbo].[NearestEvents] 
  ( 
  @lat real, 
  @long real 
  ) 
RETURNS  TABLE 
AS 
  RETURN 
  SELECT Events.ID 
  FROM   Events  
  WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 

1 个答案:

答案 0 :(得分:3)

将其添加到您的SELECT列表中。

SELECT Events.ID,
  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) as distance
  FROM   Events  
  WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 
  ORDER BY distance

- 由于它仍然不适用于OP,调试存储过程的好方法是通过劫持参数。尝试使用查询分析器,看看你得到:

DECLARE @lat = varchar(100);
DECLARE @long = vavhar(100);

SET @lat = 'XXXXX';
SET @long = 'XXXXX';

 SELECT Events.ID,
      dbo.DistanceBetween(@lat, @long, Latitude, Longitude) as distance
      FROM   Events  
      WHERE  dbo.DistanceBetween(@lat, @long, Latitude, Longitude) <30 
      ORDER BY distance;