空间查询:执行用户定义的例程或聚合“地理”期间发生 .NET Framework 错误:

Ste*_*eve 3 t-sql sql-server stored-procedures spatial-query sql-server-2016

我正在尝试从空间查询(SQL Server 2016)创建参数化存储过程。当参数(@long/经度)被硬编码时(例如 174.7115),底层空间查询工作正常。

当我尝试使用经度 ( ) 参数创建存储过程时,@long出现以下错误。

消息 6522,级别 16,状态 1,过程 Spatial8,第 5 行 [批处理起始行 0] 在执行用户定义的例程或聚合“地理”期间发生 .NET Framework 错误: System.FormatException:24141:需要一个数字输入的位置11。输入有@Long。System.FormatException:在 Microsoft.SqlServer.Types.WellKnownTextReader.RecognizeDouble() 在 Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses) 在 Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType 类型) 在 Microsoft.SqlServer.Types .WellKnownTextReader.Read(OpenGisType 类型,Int32 srid)在 Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType 类型,SqlChars taggedText,Int32 srid)在 Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType 类型,SqlChars taggedText,Int32 srid) ) ................................

这是存储过程..

CREATE PROC Spatial8 @Long decimal(9,6)
AS
DECLARE @Car geography;
SET @Car = geography::STGeomFromText ('Point(@Long -36.81143)', 4326);

/* Add 20m buffer to each side of the cars position (Lat and long) */
DECLARE @Pointbuffer geography;
SET @Pointbuffer = @Car.STBuffer ('20');

Select *, @Pointbuffer.STContains(geography ::Point(Latitude, Longitude, 4326 )) As PointBuffer
From dbo.Location
WHERE @Pointbuffer.STContains(geography ::Point(Latitude, Longitude, 4326 )) = 1
Run Code Online (Sandbox Code Playgroud)

任何意见、建议或解决方法将不胜感激。我尝试将 Geography 交换为 Geometry,但仍然遇到相同的错误。

TT.*_*TT. 5

SQL Server 不会在几何标记文本中替换参数本身。

创建一个@geometry_tagged_text类型变量NVARCHAR(MAX),将其格式化如下并将该参数传递给geography::STGeomFromText.

DECLARE @geometry_tagged_text NVARCHAR(MAX);
SET @geometry_tagged_text=N'Point('+CAST(@Long AS NVARCHAR)+N' -36.81143)';

DECLARE @Car geography;
SET @Car = geography::STGeomFromText (@geometry_tagged_text, 4326);
Run Code Online (Sandbox Code Playgroud)