存储过程部分如下:
--定义一个游标
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异常。所以我想在此循环中获取异常,如果异常发生就跳过,然后继续循环下面的数据,不能让此循环中止,因为中止后没法找到那个断点,接着循环了。先谢谢了。
--定义一个游标
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异常。所以我想在此循环中获取异常,如果异常发生就跳过,然后继续循环下面的数据,不能让此循环中止,因为中止后没法找到那个断点,接着循环了。先谢谢了。
begin
insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
exception
when dup_val_on_index then
null;
end;
end Loop;
--看看这样行么
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>
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
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
我这样试:
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;
确实出现上面的错误。
when dup_val_on_index then
...
begin
insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
end Loop;
exception
when dup_val_on_index then
null;
end;
这样再试试
begin
insert into send_box(content,usernumber)
values(content,sms.mobilephone) ;
end Loop;
exception
when others then
null;
end;
这肯定没错了……
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;
方法确实真的很好啊!再多试试!