OOTB ORACLE IFERROR函数来处理在查询中引起错误的行

发布于 2025-02-05 04:16:34 字数 817 浏览 3 评论 0 原文

在相关帖子中,@mto为

WITH FUNCTION test_from_wkbgeometry(
  v_data IN BLOB
) RETURN NUMBER
IS
  temp SDO_GEOMETRY;
BEGIN
  temp := sdo_util.from_wkbgeometry(v_data);
  RETURN 1;
EXCEPTION
  WHEN OTHERS THEN
    RETURN 0;
END;
SELECT *
FROM   my_table
WHERE  test_from_wkbgeometry(
         sdo_util.to_wkbgeometry(
           sdo_geometry(replace(sde.st_astext(shape),'LINESTRING M','LINESTRING'))
         )
       ) = 0;

该自定义函数效果很好。在查询中,如果一个给定行的函数错误,则自定义函数标记为行。

这使我可以找到问题行并确定问题是什么,而不是整个查询错误。


问题:

虽然编写自定义功能可以按预期工作,但这让我想到了,是否有任何可以达到相同目的的OOTB ORACLE功能?

例如,在Excel中,我们具有通用的IfError()函数。甲骨文有这样的东西吗?

In a related post, @MTO provided a custom function for determining what rows cause errors in a query:

WITH FUNCTION test_from_wkbgeometry(
  v_data IN BLOB
) RETURN NUMBER
IS
  temp SDO_GEOMETRY;
BEGIN
  temp := sdo_util.from_wkbgeometry(v_data);
  RETURN 1;
EXCEPTION
  WHEN OTHERS THEN
    RETURN 0;
END;
SELECT *
FROM   my_table
WHERE  test_from_wkbgeometry(
         sdo_util.to_wkbgeometry(
           sdo_geometry(replace(sde.st_astext(shape),'LINESTRING M','LINESTRING'))
         )
       ) = 0;

That custom function works well. In a query, if a function errors-out for a given row, then the custom function flags the row.

That lets me find the problem rows and determine what the issue is, instead of the entire query erroring-out.


Question:

While writing a custom function works as expected, this got me thinking, are there any OOTB Oracle functions that can serve the same purpose?

For example, in Excel, we have the generic IFERROR() function. Is there anything like that for Oracle?

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

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

发布评论

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

评论(1

无敌元气妹 2025-02-12 04:16:34

但是,通常不,有针对解析日期等特定事物的开箱即用示例:

SELECT *
FROM   table_name
WHERE  -- Ignore rows that were already null
       your_date_string IS NOT NULL
AND    -- Find the rows that would throw an error when parsing them.
       TO_DATE(
         your_date_string DEFAULT NULL ON CONVERSION ERROR,
         'YYYY-MM-DD HH24:MI:SS'
       ) IS NULL;

此语法是从Oracle 12引入的,默认值为转换错误上的默认错误,但可以更改转换错误上的默认为null 允许您查找错误。

对于示例数据:

CREATE TABLE table_name (your_date_string) AS
SELECT 'this is not a date' FROM DUAL UNION ALL
SELECT '2022-01-01 00:00:00' FROM DUAL UNION ALL
SELECT NULL FROM DUAL;

输出:

your_date_string
这不是日期

db< em>

In general no, however, there are out-of-the-box examples for specific things like parsing dates:

SELECT *
FROM   table_name
WHERE  -- Ignore rows that were already null
       your_date_string IS NOT NULL
AND    -- Find the rows that would throw an error when parsing them.
       TO_DATE(
         your_date_string DEFAULT NULL ON CONVERSION ERROR,
         'YYYY-MM-DD HH24:MI:SS'
       ) IS NULL;

This syntax was introduced from Oracle 12 and the default is DEFAULT ERROR ON CONVERSION ERROR but can be changed to DEFAULT NULL ON CONVERSION ERROR to allow you to find the errors.

Which, for the sample data:

CREATE TABLE table_name (your_date_string) AS
SELECT 'this is not a date' FROM DUAL UNION ALL
SELECT '2022-01-01 00:00:00' FROM DUAL UNION ALL
SELECT NULL FROM DUAL;

Outputs:

YOUR_DATE_STRING
this is not a date

db<>fiddle here

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