出错时那条数据还没有插入到数据库,获得存在于数据库中的重复记录的rowid好像没什么用吧,而且为此你还需要专门到数据库做一次查询操作。定位到出错的记录位置就可以了吧大批量数据处理的时候,使用forall,比如10000条。 forall i in 1 .. 10000 save exceptions insert into t values(n(i)); exception when others then dbms_output.put_line('Errors count:'||sql%bulk_exceptions.count); for i in 1 .. sql%bulk_exceptions.count loop dbms_output.put_line('index:' || sql%bulk_exceptions(i).error_index); dbms_output.put_line('ecode:' || sql%bulk_exceptions(i).error_code); end loop; end;类似于这样将错误精确定位到行关于forall,save exceptions和集合在oracle中的使用可以google下。
rowid是不会重复的,id也没有设置主键约束吧 试试 select * from(select a.*,count(1)over(partition by id)c from table1) where c>1建议id使用序列来填充,来避免重复问题
存储过程伪码:begin for()loop begin insert into tbl values (1,'a','b'); exception when others then --插入异常,不管什么,记录日志。 insert into log_temp(tb_name,pk)values('tbl',1,othervalues); end; end loop; end;
forall i in 1 .. 10000 save exceptions
insert into t values(n(i));
exception when others then
dbms_output.put_line('Errors count:'||sql%bulk_exceptions.count);
for i in 1 .. sql%bulk_exceptions.count loop
dbms_output.put_line('index:' || sql%bulk_exceptions(i).error_index);
dbms_output.put_line('ecode:' || sql%bulk_exceptions(i).error_code);
end loop;
end;类似于这样将错误精确定位到行关于forall,save exceptions和集合在oracle中的使用可以google下。
试试
select *
from(select a.*,count(1)over(partition by id)c from table1)
where c>1建议id使用序列来填充,来避免重复问题
如果是主键冲突,一眼就能看出数据库中已存在的记录是那条了。
例如:
if exists(select 1 from tb where 关键字 = 关键字的变量)
...
else
...
问题是怎样获取那个表里已有记录的rowid ?
解决方法:
1.你在插入前先查询数据库表中是否存在待插入的主键值
2.从源头避免主键的重复,建议用oacle的sequence(或自己写个方法生成不重复的id值)。
for()loop
begin
insert into tbl values (1,'a','b');
exception when others then --插入异常,不管什么,记录日志。
insert into log_temp(tb_name,pk)values('tbl',1,othervalues);
end;
end loop;
end;
仅限存储过程,如果您是jdbc的preparedStatement的executeBatch,则此法无效。因目前我还没有试出来executeBatch怎么获取批量ID。