任何人都知道如何抑制/忽略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
谢谢!
答案 0 :(得分: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
答案 1 :(得分:1)
没有;要回答你的原始问题,你不能简单地让SQL Server“忽略”错误。执行此服务器端的唯一方法是创建一个UDF,该UDF采用与 STLineFromText
相同的参数,但将调用封装在try
/ catch
块中并返回{ {1}}如果是例外。
好的,请抓一下,因为你在函数中不能null
/ try
。但是,您可以做的是定义一个UDF,它是对catch
调用存储过程的传递,该存储过程将值作为参数。然后你可以喝一杯烈酒。
有些事情......
OPENQUERY
这当然是令人憎恶的。我没有SSMS在我面前,但即使这不能编译它至少应该足以继续将它调整成一些东西。