“忽略错误”的整个概念是一个错误,如果发生任何错误都是一个谎言。这并不是说您不能捕获错误并继续处理,只是您必须处理错误。例如,假设用例:“数据已从多个 .csv 文件加载到暂存表中。现在根据 .... 加载到表 A 和表 B 中”。
create procedure
Load_Tables_A_B_from_Stage(process_message out varchar2)
is
Begin
For rec in (select * from stage)
loop
begin
insert into table_a (col1, col2)
values (rec.col_a1, col_a2);
insert into table_b (col1, col2)
values (rec.col_b1, col_b2);
exception
when others then null;
end;
end loop;
process_message := 'Load Tables A,B Complete';
end ;
现在假设用户创建了一个 .csv 文件,在其中没有值或值未知的数字列中输入“n/a”。这种太常见的情况的结果是所有此类行都没有加载,但是你无法知道直到用户抱怨他们的数据没有加载,即使你告诉他们它是。 而且你无法确定问题。
更好的方法是“捕获和报告”。
create procedure
Load_Tables_A_B_from_Stage(process_message out varchar2)
is
load_error_occurred boolean := False;
Begin
For rec in (select * from stage)
loop
begin
insert into table_a (col1, col2)
values (rec.col_a1, rec.col_a2);
exception
when others then
log_load_error('Load_Tables_A_B_from_Stage', stage_id, sqlerrm);
load_error_occurred := True;
end;
begin
insert into table_b (col1, col2)
values (rec.col_b1, rec.col_b2);
exception
when others then
log_load_error('Load_Tables_A_B_from_Stage', stage_id, sqlerrm);
load_error_occurred := True;
end;
end loop;
if load_error_occurred then
process_message := 'Load Tables A,B Complete: Error(s) Detected';
else
process_message := 'Load Tables A,B Complete: Successful No Error(s)';
end if;
end Load_Tables_A_B_from_Stage ;
现在您已将实际状态告知用户,并且与您联系的位置可以轻松识别问题。
这里的用户是在最一般的意义上使用的。这可能意味着调用例程而不是个人。重点是您不必因错误而终止进程,但不要忽略它们。