如何从sql select语句调用存储过程或存储函数

时间:2010-09-01 07:10:26

标签: sql-server-2005 tsql

我创建了以下存储的用户定义它成功执行。

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);

3 个答案:

答案 0 :(得分:3)

在SQL Server中,您需要使用模式为函数名添加前缀。

最有可能的是,你的是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

答案 1 :(得分:1)

第一个错误 - 它是USERFUNCTION而不是STOREDPROCEUDRE

第二 - 调用你必须使用的用户功能

SELECT dbo.functionName()

所以你的情况

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

答案 2 :(得分:0)

你需要加入“dbo”。在查询中的函数名称之前...