存储过程将数据类型nvarchar转换为数字时出错

时间:2014-06-13 18:20:57

标签: sql sql-server stored-procedures

我有这个存储过程:

ALTER PROCEDURE NearUnits
    @lat float,
    @lng float
AS
BEGIN
SELECT * 
FROM
(
  SELECT Uname, Latitude, Longitude,Info, 
  (
     6371 * acos(
       sin(@lat/57.295800) * 
       sin(Latitude/57.295800) + 
       cos(@lat/57.295800) * 
       cos(Latitude/57.295800) * 
       cos(@lng/57.295800 - Longitude / 57.295800)
     )
  ) AS distance 
  FROM [dbo].[MedicalUnit]
) mytable
WHERE distance <= 20;
END;

执行时:

NearUnits 31.0333449532716,31.3618322632

我收到此错误:

  

Msg 8114,Level 16,State 5,Procedure NearUnits,Line 6   将数据类型nvarchar转换为数字时出错。

虽然之前它正常工作!!!

任何解决方案??

1 个答案:

答案 0 :(得分:0)

托盘投射纬度或经度:

ALTER PROCEDURE NearUnits
    @lat float,
    @lng float
AS
BEGIN
SELECT * 
FROM
(
  SELECT Uname, Latitude, Longitude,Info, 
  (
     6371 * acos(
       sin(@lat/57.295800) * 
       sin(CAST(Latitude AS float)/57.295800) + 
       cos(@lat/57.295800) * 
       cos(CAST(Latitude AS float)/57.295800) * 
       cos(@lng/57.295800 - CAST(Longitude AS float) / 57.295800)
     )
  ) AS distance 
  FROM [dbo].[MedicalUnit]
) mytable
WHERE distance <= 20;
END;