操作数类型冲突地理与float不兼容

时间:2015-02-04 00:17:42

标签: sql-server sql-function geography operand

我试图弄清楚如何使用经度和纬度来构建一个以英里为单位测量从一个位置到另一个位置的距离的函数。运行EXECUTE时将添加参数。我试图使用“geography :: Point()”和“STDistance”而不是float来做到这一点。

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.

对于newb的任何帮助都将不胜感激。

由于

约翰

2 个答案:

答案 0 :(得分:0)

看起来函数的参数输入不正确。也就是说,如果你传递lat / long对,那些是float类型,而不是类型geography。你或许可以改变它们,让其他一切都运转起来。

答案 1 :(得分:0)

这是完成的功能对我有用,以及它的EXEC来测量从SLC,UT到LA,CA的里程数。

ALTER FUNCTION [dbo].[udfDistanceMiles] 
(
    @long1 float,
    @long2 float,
    @lat1 float,
    @lat2 float
)
RETURNS float
AS
BEGIN
    DECLARE @from geography
    DECLARE @to geography
    DECLARE @miles as float

    SET @from = geography::Point(@lat1,@long1,4326);
    SET @to = geography::Point(@lat2,@long2,4326);
    SET @miles = (SELECT ((@from.STDistance(@to) * '.001') * '.621371'));
    --SET @miles = (SELECT (@from.STDistance(@to) * '.621371'));

    RETURN @miles

END

EXEC:

USE DBM384;
GO

DECLARE @miles float= NULL;

EXEC @miles = dbo.udfDistanceMiles @lat1= 34, @long1= -118, @lat2= 40.758701, @long2= -111.876183;

PRINT @miles;