0

我创建了一个类似下面的函数

ALTER  FUNCTION fn_Calc
(@Lat1  Float,
 @Lng1  Float,
 @Lat2  Float,
 @Lng2  Float)
RETURNS Float
AS
BEGIN

Declare @x      as Float
Declare @y      as Float
Declare @Distance   as Float

Select  @x = (SIN(RADIANS(@Lat1)) * SIN(RADIANS(@Lat2)) + COS(RADIANS(@Lat1)) * COS(RADIANS(@Lat2)) * COS(ABS((RADIANS(@Lng2)) - (RADIANS(@Lng1)))))
Select  @y = ATAN((SQRT(1-(POWER(@x,2))) / @x))

Select  @Distance = (1.852 * 60.0 * ((@y / PI()) * 180)) / 1.609344

RETURN  @Distance

END

我正在使用上述函数来更新表中的列,如下所示:

Update test 
set calc = dbo.fn_Calc(cast(Lat as float), cast(Long as float), dblLat, dblLong) 

在运行上述查询时,我得到了错误。

“发生域错误。”

什么可能导致此错误?

4

1 回答 1

1

试试这个——

ALTER FUNCTION dbo.fn_Calc 
(
      @Lat1 FLOAT
    , @Lng1 FLOAT
    , @Lat2 FLOAT
    , @Lng2 FLOAT
)
RETURNS FLOAT
AS BEGIN

    DECLARE 
          @x FLOAT
        , @y FLOAT

    SELECT @x = 
                  SIN(RADIANS(@Lat1))
                * SIN(RADIANS(@Lat2)) + COS(RADIANS(@Lat1))
                * COS(RADIANS(@Lat2))
                * COS(ABS(RADIANS(@Lng2) - RADIANS(@Lng1)))

    SELECT @y = ATAN(SQRT(ABS(1 - POWER(@x, 2))) / @x)

    RETURN (111.12 * ((@y / PI()) * 180)) / 1.609344

END
于 2013-04-22T13:21:42.463 回答