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)。