如何抑制或忽略 SQL SELECT 语句中的错误

发布于 2024-10-09 06:18:24 字数 1501 浏览 0 评论 0原文

有人知道如何抑制/忽略 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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静 2024-10-16 06:18:24

所以我硬着头皮编写了自己的 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

http://msdn.microsoft.com/en-us/library/ms131065.aspx

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

你的背包 2024-10-16 06:18:24

不;要回答您最初的问题,您不能简单地让 SQL Server“忽略”错误。在服务器端执行此操作的唯一方法是创建一个 UDF,它采用与 STLineFromText 相同的参数,但将调用包含在 try/中catch 块,并在发生异常时返回 null

好吧,从头开始,因为您无法 try/catch 在函数中。不过,您可以定义一个 UDF,它是对以值作为参数的存储过程的 OPENQUERY 调用的传递。然后你就可以去喝一杯烈酒了。

类似于……

CREATE PROCEDURE SP_IgnoreErrors(@value varchar(255), @param int)
AS
BEGIN
    BEGIN TRY
        SELECT geography::STLineFromText(@value, @param) AS VALUE
    END TRY
    BEGIN CATCH
        SELECT NULL AS VALUE
    END CATCH
END

CREATE FUNCTION IgnoreErrors(@value varchar(255), @param int) RETURNS XXX
AS
BEGIN
    DECLARE @output XXX

    SELECT @output = VALUE from OPENQUERY([YOURINSTANCE],'Database.dbo.SP_IgnoreErrors ''' + @value + ''', ' + convert(varchar, @param))

    RETURN @output
END

这当然是令人憎恶的。我面前没有 SSMS,但即使这不能编译,它至少应该足以继续将其调整为可以编译的东西。

No; to answer your original question, you can't simply have SQL Server "ignore" the errors. The only way to do this server-side would be to create a UDF that takes the same parameters as STLineFromText but encloses the call in a try/catch block and returns null in the case of an exception.

OK, scratch that, since you can't try/catch in a function. What you could do, however, is define a UDF that's a pass-thru to an OPENQUERY call to a stored procedure that takes the value as a parameter. Then you can go have a stiff drink.

Something along the lines of...

CREATE PROCEDURE SP_IgnoreErrors(@value varchar(255), @param int)
AS
BEGIN
    BEGIN TRY
        SELECT geography::STLineFromText(@value, @param) AS VALUE
    END TRY
    BEGIN CATCH
        SELECT NULL AS VALUE
    END CATCH
END

CREATE FUNCTION IgnoreErrors(@value varchar(255), @param int) RETURNS XXX
AS
BEGIN
    DECLARE @output XXX

    SELECT @output = VALUE from OPENQUERY([YOURINSTANCE],'Database.dbo.SP_IgnoreErrors ''' + @value + ''', ' + convert(varchar, @param))

    RETURN @output
END

This is, of course, an abomination. I don't have SSMS in front of me, but even if this doesn't compile it should at least be enough to go on to tweak it into something that does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文