首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pl/sql:如果可能出现异常,如何放置所有行的值?

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

Stack Overflow用户
提问于 2015-03-26 20:25:22
回答 1查看 60关注 0票数 0

我有将值从一个表传递到另一个表的过程。

代码语言:javascript
运行
复制
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;

代码语言:javascript
运行
复制
tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );

可能引发异常VALUE_ERROR。

但是我需要在这段代码中插入所有的值,例如,如果异常,则插入0而不是实际值,这些值不进行转换(类似于在其他语言中尝试捕获)。

我如何在我的代码中做到这一点?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-26 20:36:58

而不是这一行:

代码语言:javascript
运行
复制
tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );

使用此子块处理异常:

代码语言:javascript
运行
复制
begin 
  tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );
exception when VALUE_ERROR
  then tn(n).num := 0;
end;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29288146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档