gpt4 book ai didi

缺少详细信息的 Oracle DML 错误

转载 作者:行者123 更新时间:2023-12-01 11:09:05 26 4
gpt4 key购买 nike

我正在从这样的批量插入操作中捕获错误:

begin
--bulk insert
forall i in v_data.first .. v_data.last save exceptions
insert into my_filter_table values v_data (i);

commit;

exception
-- catch and print the saved-up DML errors.
when X_DML_ERRORS then
declare
v_iteration number;
begin
dbms_output.put_line('');
dbms_output.put_line('DML Errors:');
for i in 1 .. SQL%BULK_EXCEPTIONS.count loop
v_iteration := SQL%BULK_EXCEPTIONS(i).error_index;

dbms_output.put_line('Iteration: '||v_iteration||' Message: '||
SQLERRM(-SQL%BULK_EXCEPTIONS(i).ERROR_CODE));


end loop;
end;
end;

输出看起来像这样:

    Iteration: 3 Message: ORA-01400: cannot insert NULL into ()    Iteration: 4 Message: ORA-02290: check constraint (.) violated    Iteration: 8 Message: ORA-00001: unique constraint (.) violated

出现错误的事实并不困扰我,因为我正在测试错误处理代码。问题是 Oracle 错误消息没有显示约束名称,即它显示 check constraint (.) violated 但这并没有告诉我我违反了哪个检查约束。

有人知道这是怎么回事吗?

(甲骨文版本 10.2)

最佳答案

SQL%BULK_EXCEPTIONS(i).error_code 只保存Oracle错误号。然后您将使用 sqlerrm 函数查找错误消息文本。该函数将无法知道打破了什么约束。

您可以在不抛出异常的情况下调用 sqlerrm 函数来复制您的结果。

begin
dbms_output.put_Line(sqlerrm(-1400));
dbms_output.put_Line(sqlerrm(-2290));
dbms_output.put_Line(sqlerrm(-1));
end;

哪些输出

ORA-01400: cannot insert NULL into ()
ORA-02290: check constraint (.) violated
ORA-00001: unique constraint (.) violated

一个可能的解决方法是在异常处理程序中重新执行失败的语句。

表定义:

create table t ( x number(1) primary key);

代码:

declare
dml_errors EXCEPTION;
PRAGMA EXCEPTION_INIT(dml_errors, -24381);
TYPE t_nums is table of NUMBER;
l_nums t_nums := t_nums(1,1,10);
begin
forall i in 1..l_nums.count save exceptions
execute immediate 'insert into t values (:x)' using l_nums(i);
exception
when dml_errors then
for j in 1..sql%bulk_exceptions.count
loop
if sql%bulk_exceptions(j).error_code = 1
then
begin
execute immediate 'insert into t values (:x)'
using l_nums(sql%bulk_exceptions(j).error_index);
exception
when dup_val_on_index then
dbms_output.put_line(sqlerrm);
end;
else
dbms_output.put_line(sqlerrm(-sql%bulk_exceptions(j).error_code));
end if;
end loop;
end;

哪些输出:

ORA-01438: value larger than specified precision allowed for this column
ORA-00001: unique constraint (XXXXXXXX.SYS_C00264470) violated
ORA-24381: error(s) in array DML

关于缺少详细信息的 Oracle DML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2149324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com