2010-09-01 173 views
1

我已经创建了以下存储用户定义它被成功执行。如何从sql select语句中调用存储过程或存储函数

CREATE FUNCTION spherical_distance1(@a float, @b float, @c float , @Lat float, @Lng float) 
RETURNS float 
AS 
BEGIN 
    RETURN (6371 * ACOS(COS(@a/@b) * COS(@Lat/@b) * COS(@Lng/@b - @c/@b) + SIN(@a/@b) * SIN(@Lat/@b)))  
END 

我面临的问题是,在这里,当我调用存储功能spherical_distance1,它显示错误,如“spherical_distance1”是无法识别的内置函数名称。

SELECT *, spherical_distance1(12.9216667, 57.2958, 77.591667, Lat, Lng) AS distance 
FROM business3 
WHERE distance < 3 
AND StreetName LIKE '%jayanagar %' 
AND Keyword LIKE '%plumbing %' 
ORDER BY spherical_distance1(12.9216667, 57.2958, 77.591667, Lat, Lng); 

回答

1

的第一个错误 - 这是一个USERFUNCTION不是一个STOREDPROCEUDRE

二 - 给你打电话必须使用

SELECT dbo.functionName() 
用户功能

因此对于您的情况

SELECT dbo.spherical_distance1(12.9216667, 57.2958, 77.591667, Lat, Lng) AS distance 
3

在SQL服务器中,需要在模式中为函数名添加前缀。

最有可能的,你的将是DBO,所以尝试调用

select *, 
    dbo.spherical_distance1(12.9216667 ,57.2958,77.591667,Lat ,Lng) as distance 
from 
    business3 
where 
    ((distance < 3) and (StreetName like '%jayanagar %') and (Keyword like '%plumbing %')) 
order by 
    distance -- don't need to repeat the function here 
0

您需要包含“dbo”。在查询函数名之前...

相关问题