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异常。

所以我想在此循环中获取异常,如果异常发生就跳过,然后继续循环下面的数据,不能让此循环中止,因为中止后没法找到那个断点,接着循环了。先谢谢了。
...全文
3519 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
  • 打赏
  • 举报
回复
习惯顶!
随着通信电子技术的迅速发展,信息技术给家居行业产生了深远的影响,家居环境的智能化监控已经成为智能家居的一个重要的发展方向。人们逐渐对自己的生活提出一种更高的要求,他们需要一种智能化、可交互,并且融合现代创新科技的产品来改善他们的生活环境,使他们生活更加安全、舒适、便捷、智能。本文根据智能家居的发展背景和研究现状,并且从实用性和可行性角度出发,研究设计了一种基于STM32单片机的智能家居系统。该系统由一个多功能综合的技术系统组成,各个多功能子系统间具有协同配合能力。基于STM32单片机实现的功能子系统包括:智能温度检测,智能湿度检测,智能烟雾/火灾检测智能检测,无线传输,人机交互机构,风扇调节,报警模块。 整个系统分为前端51单片机采集板和后端STM32单片机接收板。 采集板使用DHT11温湿度传感器、MQ烟雾传感器完成室内家居环境的采集。然后通过nRFL24L01将采集到的数据发送给后端。接收板使用LCD1602完成数据显示、使用蜂鸣器模块报警,使用风扇驱动模块调节室内家居环境。本文完成了智能家居控制系统前端、后端软硬件的设计,使用Altium designer绘制了电路原理图,使用Keil C完成了51单片机和STM32单片机的编程与调试。 最后,对本文设计的基于STM32的智能家居控制系统进行部署调试。试验结果表明,该系统可成功应用在智能家居环境检测调节和火灾安全防护的领域,可提高家居生活智能化水平。

17,377

社区成员

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

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