oracle存储过程for循环中的异常处理

bxjgood66 2006-11-23 03:36:03
存储过程部分如下:

--定义一个游标
cursor mytemp is
select id,mobilephone from mytable where trim(lanmuid) =mylanmuid and kaiguan =1;
--开始遍历游标得到其值
for sms in mytemp loop

insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
end Loop;
我的问题是send_box这个表中对字段content,usernumber作了约束unique key不能重复,且这个表有很多的程序来向其中插入数据,虽然在上面这个循环中本身的数据不会重复,但是不能保证不会和此表中已有的数据产生重复,就会产生约束unique key异常。

所以我想在此循环中获取异常,如果异常发生就跳过,然后继续循环下面的数据,不能让此循环中止,因为中止后没法找到那个断点,接着循环了。先谢谢了。
...全文
3426 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wvivw 2007-02-07
  • 打赏
  • 举报
回复
其实这个问题应该很好解决
你可以在一个过程先打开游标,然后调用插入的哪个过程
如果插入的哪个过程出错的话,跳出插入的过程,游标的过程继续运行
gwallan 2006-11-29
  • 打赏
  • 举报
回复
mark
tongyu10068 2006-11-27
  • 打赏
  • 举报
回复
for sms in mytemp loop
begin
insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
end Loop;
exception
when dup_val_on_index then
null;
end;
这样再试试
tianyacao007 2006-11-27
  • 打赏
  • 举报
回复
icedut(冰) ( ) 信誉:100 Blog
方法确实真的很好啊!再多试试!
tianyacao007 2006-11-27
  • 打赏
  • 举报
回复
在函数中增加一个判断唯一性得函数:
cursor mytemp is
select id,mobilephone from mytable where trim(lanmuid) =mylanmuid and kaiguan =1;
--开始遍历游标得到其值
for sms in mytemp loop
--判断content,usernumber是否在 send_box中是否存在
if is_exists_send_box(content,usernumber) = 1 then
--若存在退出这次循环
exit;
end if;

insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
end Loop;
AFIC 2006-11-27
  • 打赏
  • 举报
回复
for sms in mytemp loop
begin
insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
end Loop;
exception
when others then
null;
end;
这肯定没错了……
tianya0801 2006-11-26
  • 打赏
  • 举报
回复
exception
when dup_val_on_index then
...
liuxijue 2006-11-26
  • 打赏
  • 举报
回复
在插入之前用content,usernumber 作检索吧 对机能影响应该不大
bxjgood66 2006-11-24
  • 打赏
  • 举报
回复
up
bxjgood66 2006-11-24
  • 打赏
  • 举报
回复
还有什么办法吗?
bxjgood66 2006-11-23
  • 打赏
  • 举报
回复
问题还没有解决
我这样试:
for sms in mytemp loop
begin
insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
exception
when dup_val_on_index then
null;
end;
end Loop;
确实出现上面的错误。
icedut 2006-11-23
  • 打赏
  • 举报
回复
上面的兄弟:我试过了 出错 如下:

--有错误也是你别的地方的错误
icedut 2006-11-23
  • 打赏
  • 举报
回复
begin
for sms in (select * from b_areas) loop
begin
--if (sms.area_id='M') then
dbms_output.put_line(sms.area_id);
--else
-- raise dup_val_on_index ;
--end if;
--exception
-- when dup_val_on_index then
-- dbms_output.put_line('null');
-- null;
end;
end Loop;
end;
--
N
H
X
O
E
S
M
W
icedut 2006-11-23
  • 打赏
  • 举报
回复
begin
for sms in (select * from b_areas) loop
begin
if (sms.area_id='M') then
dbms_output.put_line(sms.area_id);
else
raise dup_val_on_index ;
end if;
exception
when dup_val_on_index then
dbms_output.put_line('null');
null;
end;
end Loop;
end;
--
null
null
null
null
null
null
M
null
bxjgood66 2006-11-23
  • 打赏
  • 举报
回复
上面的兄弟:我试过了 出错 如下:

PLS-00103: 出现符号 "EXCEPTION"在需要下列之一时:
begin case declare
end exit for goto if loop mod null pragma raise return select
update while with <an identifier>
<a double-quoted delimited-identifier>
icedut 2006-11-23
  • 打赏
  • 举报
回复
for sms in mytemp loop
begin
insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
exception
when dup_val_on_index then
null;
end;
end Loop;
--看看这样行么
bxjgood66 2006-11-23
  • 打赏
  • 举报
回复
习惯顶!

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧