pl/sql: 如果可能出现异常,如何放置所有行的值?

pl/sql: how to put the values of all rows if exceptions is possible?

我有将值从一个 table 转移到另一个的程序。

create table t_num(num number(10,2));
create table t_str(str varchar2(10));
insert into t_str (str) values('23');
insert into t_str (str) values('2 3');
insert into t_str (str) values('2 3,3 2');
insert into t_str (str) values('2 3,3 223');
commit;

create or replace procedure put_to_t_num
as
    type t_num_t is table of t_num%rowtype index by binary_integer;
    tn t_num_t;
    n binary_integer := 0;
begin
    delete from t_num;
    --tn := t_num_t();
    for rec in ( select * from t_str )
    loop
        n := n + 1;
        --tn.extend;
        tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );
    end loop;

    forall i in 1..n
        insert into t_num (
            num
        ) values (
            tn(i).num
        );
        --commit;
end;

字符串

tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );

可能会抛出异常VALUE_ERROR。

但我需要在此代码中插入所有值,例如如果出现异常则插入 0 而不是实际值,不会转换(类似于其他语言中的 try-catch)。

如何在我的代码中执行此操作?

谢谢!

而不是这一行:

tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );

使用这个子块来处理异常:

begin 
  tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );
exception when VALUE_ERROR
  then tn(n).num := 0;
end;