引发异常时在 Oracle 中继续插入

2021-12-24 00:00:00 sql exception-handling oracle plsql

我正在将数据从旧系统迁移到我们的新应用程序(在 Oracle 数据库 10gR2 上运行).作为迁移的一部分,我正在编写一个脚本,该脚本将数据插入到应用程序使用的表中.

I'm working on migration of data from a legacy system into our new app(running on Oracle Database, 10gR2). As part of the migration, I'm working on a script which inserts the data into tables that are used by the app.

导入的数据行数达到数千,源数据不干净(NOT NULL 列中出现意外的空值等).所以在通过脚本插入数据的时候,每当发生这样的异常时,脚本会突然结束,整个事务被回滚.

The number of rows of data that are imported runs into thousands, and the source data is not clean (unexpected nulls in NOT NULL columns, etc). So while inserting data through the scripts, whenever such an exception occurs, the script ends abruptly, and the whole transaction is rolled back.

有没有办法可以继续插入行干净的数据?使用 NVL()COALESCE() 不是一种选择,因为我想记录导致错误的行,以便可以在下一次通过时更正数据.

Is there a way, by which I can continue inserts of data for which the rows are clean? Using NVL() or COALESCE() is not an option, as I'd like to log the rows causing the errors so that the data can be corrected for the next pass.

我当前的过程有一个异常处理程序,我正在记录导致错误的第一行.插入是否可以继续而不终止,因为现在在第一个处理的异常处,过程终止执行.

My current procedure has an exception handler, I am logging the first row which causes the error. Would it be possible for inserts to continue without termination, because right now on the first handled exception, the procedure terminates execution.

推荐答案

使用 PLSQL,您可以在其自己的事务中执行每个插入(在每个之后提交),并使用持续运行的异常处理程序记录或忽略错误.

Using PLSQL you can perform each insert in its own transaction (COMMIT after each) and log or ignore errors with an exception handler that keeps going.

相关文章