0

我有以下查询,如果我注释掉导致我的问题的部分(正如我在下面已经完成的那样),它就可以正常工作。

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 行,其中一行的 aPriorityType为 0。

问题是,Microsoft SQL Server Management Studio(简称 SSMS)给了我错误

Msg 207, Level 16, State 1, Line 13
Invalid column name 'Distance'.

在 的“距离”下方有一条红线AND Distance <= @iRadius

如果我注释掉该行(如上面的示例),我会根据需要获得整个表格。显然,该列存在!那么为什么我会收到错误消息?

4

1 回答 1

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;

于 2019-04-09T11:38:52.247 回答