想请教一下关于oracle异常处理的问题
我写了一个存储过程,
在loop游标的时候,需要作一下判断,当游标中某一字段的值包含“N”这个字符时,则进行插入,为"U"则更新。在插入时由于可能出现主键冲突,需要将所有有问题的数据插入其他另外一张表里,正常的数据则继续插入,请问这个可以实现吗?谢谢
我写了一个存储过程,
在loop游标的时候,需要作一下判断,当游标中某一字段的值包含“N”这个字符时,则进行插入,为"U"则更新。在插入时由于可能出现主键冲突,需要将所有有问题的数据插入其他另外一张表里,正常的数据则继续插入,请问这个可以实现吗?谢谢
begin
insert ....;
commit;
exception when others then
rollback;
insert into other_table...;
commit;
end;
CURSOR cur_ics_tkt IS
select ... from ics_tkt_back t; passengerid varchar(20);
....
begin
open cur_ics_tkt;
LOOP
fetch cur_ics_tkt
into passengerid,
....;
IF (cur_ics_tkt%FOUND) THEN
statusnum:=instr(status, 'N');
IF (statusnum > 0) THEN --如果状态值里有N,则表示NEW数据,即新增
insert into ics_tkt
....
ELSIF (statusnum < 1) THEN --反之有两种情况
statusnum:=substr(status, 10, 1);
IF (statusnum = 1) THEN --倒数第二位数字为1,则表示删除该条交易
delete from ics_tkt t
....
ELSE --不是则表示更新
update ics_tkt t
....
end if;
end if;
else
exit;
end if;
END LOOP;
commit;
end PROC_ICS_TKT;
这就是我大概的代码,请问我应该把exception放到哪儿?谢谢
...
loop
begin
insert into ...//正常作业
exception//声明异常
where others then null;
insert into ... //将出现异常的数据插入到表
end;
end loop;
commit;
end;
CURSOR cur_ics_tkt IS
select ... from ics_tkt_back t; passengerid varchar(20);
....
begin
open cur_ics_tkt;
LOOP
fetch cur_ics_tkt
into passengerid,
....;
begin
IF (cur_ics_tkt%FOUND) THEN
statusnum:=instr(status, 'N');
IF (statusnum > 0) THEN --如果状态值里有N,则表示NEW数据,即新增
insert into ics_tkt
....
ELSIF (statusnum < 1) THEN --反之有两种情况
statusnum:=substr(status, 10, 1);
IF (statusnum = 1) THEN --倒数第二位数字为1,则表示删除该条交易
delete from ics_tkt t
....
ELSE --不是则表示更新
update ics_tkt t
....
end if;
end if;
else
exit;
end if;
exception --声明异常
where others then null;
insert into ... --将出现异常的数据插入到表
end;
END LOOP;
commit;
end PROC_ICS_TKT;
这边是因为主键冲突而导致异常,插入另一个表
要是这边不是因为主键冲突而发生异常,并且插入另一个表成功。会不会是个bug?
我觉得插入之前查一遍,存在了就不插,并且换插另外一个表,不存在就插入。