ORA-31061: XDB error: special char to escaped char conversion failed

ORA-31061: XDB error: special char to escaped char conversion failed

我完全不熟悉 Oracle 的 XDB,试图在我完全不熟悉的生产环境中修复代码。

SELECT  DBMS_LOB.SubStr(RTRIM (XMLAGG (XMLELEMENT (E,XMLATTRIBUTES (ntx.nttx_str || '|' AS "Seg")) ORDER BY ntx.ref_id, ntx.ntnb_input_dtm desc)
                                   .EXTRACT ('./E[not(@Seg = preceding-sibling::E/@Seg)]/@Seg').GetClobVal(),','),1000,1)
    FROM
       (SELECT umum.ref_id,
               ntnb.ntnb_input_dtm,
               DBMS_LOB.SubStr(RTRIM (XMLAGG (XMLELEMENT (E,XMLATTRIBUTES (nttx.nttx_text || '' AS "Seg")) ORDER BY nttx.nttx_seq_no asc)
                                   .EXTRACT ('./E[not(@Seg = preceding-sibling::E/@Seg)]/@Seg').GetClobVal(),','),500,1) AS nttx_str
        FROM umum_util_mgt umum,
             ntnb_note_base ntnb,
             nttx_note_text nttx
        WHERE ntnb.ntnb_id = umum.ntnb_id
          AND ntnb.ntnb_id = nttx.ntnb_id
          AND ntnb.ntnb_input_dtm = nttx.ntnb_input_dtm
          GROUP BY umum.ref_id, ntnb.ntnb_input_dtm
        ) ntx,
        php_mdx_auth_ext_tbl paex
        WHERE ntx.ref_id = paex.ref_id*

当我尝试执行上述查询时,Oracle 返回以下错误

ORA-31061: XDB 错误: 特殊字符到转义字符的转换失败。

这曾经在旧版本的 Oracle 中工作,在迁移到 oracle 12c 后我们面临这个问题。

当 XML 数据中存在控制字符(错误数据)时,通常会出现此错误。

我能够用错误的 XML 数据重现错误,但是是的,错误代码不同,我不确定为什么。

SQL> WITH DATAA AS (
  2      SELECT UNISTR('SO[=10=]13bad') TEST FROM DUAL
  3      UNION ALL
  4      SELECT UNISTR('SO[=10=]aegood') TEST FROM DUAL
  5  )
  6  SELECT xmlelement("a", test) AS TEST
  7    FROM DATAA;
ERROR:
ORA-64451: Conversion of special character to escaped character failed.

要解决此错误,

  1. 您需要找出包含错误 XML 数据的记录并更正这些数据。您可以使用 REGEXP_LIKE 如下所示查找错误的 XML 数据并更正这些数据:
SQL> WITH DATAA AS (
  2      SELECT UNISTR('SO[=11=]13bad') TEST FROM DUAL
  3      UNION ALL
  4      SELECT UNISTR('SO[=11=]aegood') TEST FROM DUAL
  5  )
  6  SELECT *
  7  FROM DATAA
  8  WHERE REGEXP_LIKE ( TEST, '[[:cntrl:]]' );

TEST
-------
SObad

SQL>

-- 或--

  1. 您可以使用 REGEXP_REPLACE 跳过处理 XML 数据中的控制字符,如下所示:
SQL> WITH DATAA AS (
  2      SELECT UNISTR('SO[=12=]13bad') TEST FROM DUAL
  3      UNION ALL
  4      SELECT UNISTR('SO[=12=]aegood') TEST FROM DUAL
  5  )
  6  SELECT xmlelement("a", REGEXP_REPLACE(TEST, '[[:cntrl:]]', '')) AS TEST
  7    FROM DATAA;

TEST
--------------------------------------------------------------------------------
<a>SObad</a>
<a>SO«good</a>

SQL>

干杯!!