STDistance – 计算两个对象之间的距离

STDistance – Calculates distance between two objects

我试着计算两个物体之间的距离。

declare @p1 geography
declare @p2 geography
SELECT @p1 = WKT from tbl_1 where loc = "school"
SELECT @p2 = WKT from tbl_2 where loc = "school"
select round(@p1.STDistance(@p2)/1000,0) Distance_KM

但是我收到列 loc

的错误

Invalid column name

此列存在且数据类型为geography

WKT 列使用以下方法填充:

UPDATE [dbo].[lbl_1]
SET [WKT] = geography::STPointFromText('POINT(' + CAST([Longitude] AS VARCHAR(20)) + ' ' + CAST([Latitude] AS VARCHAR(20)) + ')', 4326)
GO

怎么了??

您的字符串文字不正确。

对于 SQL,您需要单引号,即 'school' 而不是 "school"

SQL 将其视为列而不是字符串文字。