无法在存储过程中调用函数

时间:2019-08-17 10:29:09

标签: sql-server tsql stored-procedures stored-functions

在存储过程中调用我的函数时遇到问题。

下面,我创建了一个函数:makePoint。我想在存储过程中使用它

select * from my_table where dbo.makePoint(lon, lat).STWithin(my polygon)

但是我不能使用该功能。

错误消息:

  

该功能是找不到用户定义的功能“ makePoint”。

我想念什么?我已经在dbo中创建了SP和功能。

-- ================================================
-- Template generated from Template Explorer using:
-- Create Inline Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE OR ALTER FUNCTION dbo.makePoint(@lon FLOAT, @lat FLOAT)  
RETURNS Geometry   
AS   
BEGIN  
    DECLARE @g Geometry   
    SET @g = Geometry::Point(@lon, @lat, 4326)  

    RETURN @g
END
GO

和我的存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER PROCEDURE dbo.spGetItemsInRegion @regionId INT
AS
BEGIN
    SET NOCOUNT ON
    declare @geo as Geometry
    select @geo = polygon from something where id = @regionId

    select * from my_table where dbo.makePoint(lon, lat).STWithin(@geo)    
END
GO

2 个答案:

答案 0 :(得分:2)

STWithin返回0或1,因此需要将函数结果与这些值之一进行比较。

(请注意1和0在T-SQL中的行为不等于布尔值)

修改后的代码:

select * from my_table
where dbo.makePoint(lon, lat).STWithin(@geo) = 1

除了代码应该可以正常工作外,请参见dbfiddle

答案 1 :(得分:0)

请检查您的情况,如下所示

SELECT * FROM my_table WHERE someting= dbo.makePoint(lon, lat).STWithin(@geo);
相关问题