我试图弄清楚如何构建一个函数,使用经度和纬度测量从一个位置到另一个位置的距离(以英里为单位)。运行 EXECUTE 时将添加参数。我正在尝试使用“geography::Point()”和“STDistance”而不是浮点数来做到这一点。
IF OBJECT_ID('dbo.udfDistanceMiles') IS NOT NULL
DROP FUNCTION dbo.udfDistanceMiles
GO
CREATE FUNCTION dbo.udfDistanceMiles
(
@long1 geography,
@long2 geography,
@lat1 geography,
@lat2 geography
)
RETURNS geography
AS
BEGIN
DECLARE @from geography
DECLARE @to geography
DECLARE @kilo geography
DECLARE @miles as geography
SET @from = geography::Point(@lat1,@long1,4268);
SET @to = geography::Point(@lat2,@long2,4268);
SET @kilo = (SELECT @from.STDistance(@to));
BEGIN
SET @miles = (@kilo * '.621371');
END
RETURN @miles
END
GO
这是我正在参加的空间数据库课程的作业,但我遇到了一个我无法弄清楚的障碍。我在尝试创建函数时遇到了这种操作数类型冲突:
Msg 206, Level 16, State 2, Procedure udfDistanceMiles, Line 19
Operand type clash: geography is incompatible with float
Msg 206, Level 16, State 2, Procedure udfDistanceMiles, Line 19
Operand type clash: geography is incompatible with float
Msg 206, Level 16, State 2, Procedure udfDistanceMiles, Line 20
Operand type clash: geography is incompatible with float
Msg 206, Level 16, State 2, Procedure udfDistanceMiles, Line 20
Operand type clash: geography is incompatible with float
Msg 206, Level 16, State 2, Procedure udfDistanceMiles, Line 21
Operand type clash: float is incompatible with geography
Msg 403, Level 16, State 1, Procedure udfDistanceMiles, Line 23
Invalid operator for data type. Operator equals multiply, type equals geography.
对新手的任何帮助将不胜感激。
谢谢
约翰