我有以下查询,如果我将引起问题的部分注释掉(如我在下面已经做过的话),该查询就可以正常工作。
Declare @mPoint As varchar(50) = 'POINT (-107.657141 41.033581)'
Declare @iRadius As int = 5000 --5km for testing. 4 results, 1 with PriorityType = 0.
SELECT FLOOR([Position].STDistance(geography::STGeomFromText(@mPoint, 4326))) AS Distance,
CASE
WHEN [Type] & 64 = 64 THEN 0
--insert other types as needed.
ELSE 1000
END AS PriorityType,
*
FROM [tblAddress]
WHERE DeletedOn IS NULL
--AND Distance <= @iRadius -- <-- This is Line 13
ORDER BY PriorityType ASC, Distance ASC;
此查询应过滤数据库中约1700条记录,并根据上述(静态)结果,返回4行,其中PriorityType
为0。
问题是,Microsoft SQL Server Management Studio(简称SSMS)给了我错误
Msg 207, Level 16, State 1, Line 13
Invalid column name 'Distance'.
在AND Distance <= @iRadius
的“距离”下方有一条红线。
如果我注释掉该行(如上例所示),则可以根据需要获取整个表格。显然,该列存在!那么为什么我会收到错误消息?
答案 0 :(得分:0)
检查此修改后的解决方案,
Declare @mPoint As varchar(50) = 'POINT (-107.657141 41.033581)'
Declare @iRadius As int = 5000
SELECT FLOOR([Position].STDistance(geography::STGeomFromText(@mPoint, 4326))) AS Distance,
CASE WHEN [Type] & 64 = 64 THEN 0
ELSE 1000 END AS PriorityType,
*
FROM [tblAddress]
WHERE DeletedOn IS NULL AND FLOOR([Position].STDistance(geography::STGeomFromText(@mPoint, 4326)))<= @iRadius
ORDER BY PriorityType ASC, Distance ASC;