Anyone know how to suppress / ignore errors in a select statement?
My select statement uses the STLineFromText function, and when it hits an invalid line, it raises an error. I'm ok with ignoring the bad rows, and cannot really change my source data.
Here is a sample query that demonstrates my problem:
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)
and here is the error:
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)
The ideal situation would be to just return #1, #2, #4 & #5, ignoring #3
Thanks!
So I bit the bullet and wrote my own CLR function so that I could incorporate a try/catch. It's working pretty well.
<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
Credits:
http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/ms131065.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With