有人知道如何抑制/忽略 select 语句中的错误吗?
我的 select 语句使用 STLineFromText 函数,当它遇到无效行时,会引发错误。我可以忽略坏行,并且无法真正更改我的源数据。
这是一个演示我的问题的示例查询:
SELECT geography::STLineFromText('LINESTRING(-74.2204037952351 40.4283173372472,-74.2204851952350 40.4283519372471)', 4326) UNION ALL
SELECT geography::STLineFromText('LINESTRING(-74.2316367952177 40.4386102038979,-74.2313671952181 40.4388540705641)', 4326) UNION ALL
SELECT geography::STLineFromText('LINESTRING(-74.2229282618978 40.4252709372519,-74.2229171285645 40.4252638039186,-74.2229282618978 40.4252709372519,-74.2227441952315 40.4251499372521,-74.2231121285642 40.4243291372534)', 4326) UNION ALL
SELECT geography::STLineFromText('LINESTRING(-74.2418989952017 40.4417621372263,-74.2417773285352 40.4417915372263)', 4326) UNION ALL
SELECT geography::STLineFromText('LINESTRING(-74.2166069952410 40.4334496039059,-74.2158269952422 40.4336396039056)', 4326)
这是错误:
Msg 6522, Level 16, State 1, Line 2
A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
System.ArgumentException: 24200: The specified input does not represent a valid geography instance.
System.ArgumentException:
at Microsoft.SqlServer.Types.SqlGeography.ConstructGeographyFromUserInput(GeoData g, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
理想的情况是只返回 #1、#2、#4 和 #5,忽略 #3
谢谢!
最佳答案
所以我硬着头皮编写了自己的 CLR 函数,以便可以合并 try/catch。它运行得很好。
<Microsoft.SqlServer.Server.SqlFunction()> _
Public Shared Function STLineFromTextFlexible(ByVal LineString As SqlChars, ByVal SRID As Integer) As Microsoft.SqlServer.Types.SqlGeography
Try
Dim Geo As New SqlGeography()
Geo = SqlGeography.STLineFromText(LineString, SRID)
Return Geo
Catch ex As Exception
Return Nothing
End Try
End Function
鸣谢:
http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx
关于.net - 如何抑制或忽略 SQL SELECT 语句中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527508/