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

时间:2016-11-12 01:34:32

标签: sql-server tsql stored-procedures sql-server-2016 spatial-query

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

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

  

Msg 6522,Level 16,State 1,Procedure Spatial8,Line 5 [Batch Start Line 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)   .........................

这是Stored Proc ..

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,但我仍然遇到同样的错误。

1 个答案:

答案 0 :(得分:1)

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