Tom*_*day 5 .net sql t-sql sql-server error-handling
任何人都知道如何抑制/忽略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)
Run Code Online (Sandbox Code Playgroud)
这是错误:
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)
Run Code Online (Sandbox Code Playgroud)
理想的情况是返回#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
Run Code Online (Sandbox Code Playgroud)
学分:
http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/ms131065.aspx