1
CREATE TABLE test (c1 NUMBER(10) NOT NULL);

exec DBMS_ERRLOG.CREATE_ERROR_LOG ( 'test', 'err_test');

test我想将其他表中的一些值插入表中,并记录错误。我还想记录无法插入到 err_test 表中的记录的 ID。在伪代码中,这将导致如下所示:

INSERT INTO test(c1)
SELECT some_nr AS special_nr FROM some_table_which_has_an_unique_id_for_each_number
LOG ERRORS INTO err_test('Could not insert record with id:' || some_table.id /* where id is the id number of the special_nr */ ) REJECT LIMIT UNLIMITED;

以上显然是行不通的,因为src.id是未知的。但是我可以使用充当插入语句的合并语句来执行上述插入:

MERGE INTO test trg
USING (SELECT NULL AS special_nr, 17 AS id FROM DUAL ) src
ON      (1 = 2) -- force INSERTS (!!) Not using INSERT statement because in err log extra data is needed.
WHEN NOT MATCHED THEN 
        INSERT  (trg.c1)
        VALUES  (src.special_nr)
LOG ERRORS INTO err_test('Could not insert record with id:' || src.id) REJECT LIMIT UNLIMITED;

执行上述后,错误并没有记录到err_table中,而是抛给了用户:

    ORA-01489: result of string concatenation is too long
    ORA-01400: cannot insert NULL into ("SCV_DPN"."TEST"."C1")

字符串太长?然后我尝试不使用 err_mesg$ 中的连接:

MERGE INTO test trg
USING (SELECT NULL AS special_nr, 17 AS id FROM DUAL ) src
ON      (1 = 2) -- force INSERTS (!!) Not using INSERT statement because in err log extra data is needed.
WHEN NOT MATCHED THEN 
        INSERT  (trg.c1)
        VALUES  (src.special_nr)
LOG ERRORS INTO err_test(src.id) REJECT LIMIT UNLIMITED;

奥赫:

    ORA-38908: internal error occurred during DML Error Logging
    ORA-01024: invalid datatype in OCI call
    ORA-01400: cannot insert NULL into ("SCV_DPN"."TEST"."C1")      

它变得更好:

MERGE INTO test trg
USING (SELECT NULL AS special_nr, 17 AS id FROM DUAL ) src
ON      (1 = 2) -- force INSERTS (!!) Not using INSERT statement because in err log extra data is needed.
WHEN NOT MATCHED THEN 
        INSERT  (trg.c1)
        VALUES  (src.special_nr)
LOG ERRORS INTO err_test('Could not insert record with id:' || nvl(src.id, -1)) REJECT LIMIT UNLIMITED;

现在我被踢了:

    ORA-03113: end-of-file on communication channel
    Process ID: 25352
    Session ID: 171 Serial number: 979  

这里发生了什么?似乎1 = 2是罪魁祸首,因为

MERGE INTO test trg
USING (SELECT NULL AS special_nr, 17 AS id FROM DUAL ) src
ON      (src.id IS NULL) -- force INSERTS (!!) Not using INSERT statement because in err log extra data is needed.
WHEN NOT MATCHED THEN 
        INSERT  (trg.c1)
        VALUES  (src.special_nr)
LOG ERRORS INTO err_test('Could not insert record with id:' || src.id) REJECT LIMIT UNLIMITED;

正常工作,并且 (!!) 将错误记录在err_table

    ORA_ERR_NUMBER$     ORA_ERR_MESG$                                                   ORA_ERR_ROWID$  ORA_ERR_OPTYP$  ORA_ERR_TAG$                            C1
    1400                ORA-01400: cannot insert NULL into ("SCV_DPN"."TEST"."C1")                      I               Could not insert record with id:17          

现在它可以正常工作了。

有人可以向我解释这里发生了什么和/或还提供一种更好的方法来解决上述问题,即必须在ORA_ERR_MESG$字段中记录来自源表的额外信息。

注意:根据我可以使用的文档1 = 2要将所有源行插入表中,您可以在 ON 子句条件中使用常量过滤谓词。一个常量过滤谓词的例子是 ON (0=1)。

4

1 回答 1

1

我相信您错过了一个可能的罪魁祸首:在 tag 参数中包含行值。我不认为此功能旨在提供您尝试使用它的功能。虽然我找不到任何有关此效果的文档,但对初始merge(具有串联错误的那个)的简单更改似乎证实了这一点:

MERGE INTO test trg
USING (SELECT NULL AS special_nr, 17 AS id FROM DUAL ) src
ON      (1 = 2) -- force INSERTS (!!) Not using INSERT statement because in err log extra data is needed.
WHEN NOT MATCHED THEN 
        INSERT  (trg.c1)
        VALUES  (src.special_nr)
LOG ERRORS INTO err_test('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') REJECT LIMIT UNLIMITED;

尽管标签超出了最初提供的连接值的长度,但此 SQL 是成功的。

为了获得您想要的更详细的日志记录,我建议使用包含插入的游标 for 循环。它可能会稍微慢一些,但它可以让您在出现错误时记录尽可能多的详细信息。


文档似乎支持这一结论,尽管它比直接陈述更为隐含。在描述允许错误记录的“简单表达式”时,它指出:“表达式可以是文本文字、数字文字或通用 SQL 表达式,例如绑定变量。”

于 2014-09-18T13:32:38.597 回答