空间查询:执行用户定义的例程或聚合时发生 .NET Framework 错误 "geography":

Spatial query: A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":

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

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

Msg 6522, Level 16, State 1, Procedure Spatial8, Line 5 [Batch Start Line 0] A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": System.FormatException: 24141: A number is expected at position 11 of the input. The input has @Long. System.FormatException: at Microsoft.SqlServer.Types.WellKnownTextReader.RecognizeDouble() at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses) at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type) at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid) at Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType type, SqlChars taggedText, Int32 srid) at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, 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

如有任何意见、建议或解决方法,我们将不胜感激。我试过将 Geography 换成 Geometry,但我仍然遇到同样的错误。

SQL 服务器不会自行替换几何标记文本中的参数。

创建一个 @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);