在存储过程中调用我的函数时出现问题。
下面我创建了一个函数: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
> STWithin 返回 0 或 1,因此您需要将函数结果与其中一个值进行比较。
(请注意,1 和 0 在 T-SQL 中的行为不像布尔值(
修改后的代码:
select * from my_table
where dbo.makePoint(lon, lat).STWithin(@geo) = 1
除此之外,代码应该可以正常工作,请参阅 dbfiddle。
请检查您的位置条件,如下所示
SELECT * FROM my_table WHERE someting= dbo.makePoint(lon, lat).STWithin(@geo);