oracle 的存储过程错误处理怎么做?刚学多多关照

Tuxedo10 2003-09-08 08:51:33

大家帮忙看看怎么回事!!!

create or replace procedure importtable is
--声明变量
bcustomerid tf_f_usrarch_main.customerid%type;
bmphonecode tf_f_usrarch_main.mphonecode%type;
bsimcardno tf_f_usrarch_main.simcardno%type;
bopendate tf_f_usrarch_main.opendate%type;
bassurename tf_f_usrarch_main.assurename%type;
bassurepid tf_f_usrarch_main.assurepid%type;
bassurelink tf_f_usrarch_main.assurelink%type;
bassuretypecode tf_f_usrarch_main.assuretypecode%type;
bremovetag tf_f_usrarch_main.removetag%type;
bdestroytime tf_f_usrarch_main.destroytime%type;
btradetypecode tf_f_usrarch_main.tradetypecode%type;
bdepartid tf_f_usrarch_main.departid%type;
ausrname tf_f_customer.usrname%type;
apassporttypecode tf_f_customer.passporttypecode%type;
ausrpid tf_f_customer.usrpid%type;
ausraddress tf_f_customer.usraddress%type;
apostcode tf_f_customer.postcode%type;
alinkhomephone tf_f_customer.linkhomephone%type;
alinkman tf_f_customer.linkman%type;
cfeesetcode tf_f_usrarch_feeset.feesetcode%type;
--先找到开户时间和修改时间一样的,insert
--再找开户时间和修改时间不一样的,update
cursor cur_equlas_openup is--找到开户时间和修改时间一样的,
select b.customerid,
b.mphonecode,b.simcardno,b.opendate,b.assurename,b.assurepid,b.assurelink,b.assuretypecode,
b.removetag,b.destroytime,b.tradetypecode,b.departid,
a.usrname,a.passporttypecode,a.usrpid,a.usraddress,a.postcode,
a.linkhomephone,a.linkman,
c.feesetcode
from tf_f_customer a,tf_f_usrarch_main b,tf_f_usrarch_feeset c
where a.customerid=b.customerid and c.usrid=b.usrid and
b.opendate=b.updatetime;

cursor cur_def_openup is--再到开户时间和修改时间不一样的,
select b.customerid,
b.mphonecode,b.simcardno,b.opendate,b.assurename,b.assurepid,b.assurelink,b.assuretypecode,
b.removetag,b.destroytime,b.tradetypecode,b.departid,
a.usrname,a.passporttypecode,a.usrpid,a.usraddress,a.postcode,
a.linkhomephone,a.linkman,
c.feesetcode
from tf_f_customer a,tf_f_usrarch_main b,tf_f_usrarch_feeset c
where a.customerid=b.customerid and c.usrid=b.usrid and
b.opendate<>b.updatetime;

begin
--找到开户时间和修改时间一样的,
open cur_equlas_openup ;
fetch cur_equlas_openup into
bcustomerid,bmphonecode,bsimcardno,bopendate,bassurename,bassurepid,
bassurelink,bassuretypecode,bremovetag,bdestroytime,btradetypecode,bdepartid,
ausrname,apassporttypecode,ausrpid,ausraddress,apostcode,alinkhomephone,alinkman,
cfeesetcode;
while cur_equlas_openup%found loop
--插入
insert into zwdjdxg z(z.djbh,z.gxcs,z.workerid,z.ywlbid,z.mid,
z.dhhm,z.dhkh,z.blrq,z.dbr,z.dbrzjhm,
z.dbrlxdh,z.dbrzjlxid,z.removetag,z.destroytime,z.tradetypecode,z.deptid,
z.khmc,z.zjlxid,z.zjhm,z.txdz,z.yzbm,z.lxdh1,z.lxr,
z.tclxid) values(
bcustomerid,'99','8888','WXGH','SH002',
bmphonecode,bsimcardno,bopendate,bassurename,bassurepid,
bassurelink,bassuretypecode,bremovetag,bdestroytime,btradetypecode,bdepartid,
ausrname,apassporttypecode,ausrpid,ausraddress,apostcode,alinkhomephone,alinkman,
cfeesetcode
);

commit;
--错误处理
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
--插入日志
insert into aaaa(aaaa,bbbb) values ('ss','ss01');

end loop;
--再到开户时间和修改时间不一样的,
open cur_def_openup ;
fetch cur_def_openup into
bcustomerid,bmphonecode,bsimcardno,bopendate,bassurename,bassurepid,
bassurelink,bassuretypecode,bremovetag,bdestroytime,btradetypecode,bdepartid,
ausrname,apassporttypecode,ausrpid,ausraddress,apostcode,alinkhomephone,alinkman,
cfeesetcode;
while cur_def_openup%found loop
insert into zwdjdxg a select * from zwdjd b where b.djbh=bcustomerid;
delete from zwdjd b where b.djbh=bcustomerid;

update zwdjdxg z set
z.dhkh=bsimcardno,z.blrq=bopendate,z.dbr=bassurename,
z.dbrzjhm=bassurepid,z.dbrlxdh=bassurelink,z.dbrzjlxid=bassuretypecode,
z.removetag=bremovetag,z.destroytime=btradetypecode,
z.tradetypecode=btradetypecode,z.deptid=bdepartid,
z.khmc=ausrname,z.zjlxid=apassporttypecode,z.zjhm=ausrpid,
z.txdz=ausraddress,z.yzbm=apostcode,z.lxdh1=alinkhomephone,z.lxr=alinkman,
z.tclxid=cfeesetcode
where z.djbh=bcustomerid;
end loop;
end importtable;


问题在于我写那段异常处理的时候执行不过去!!
...全文
282 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LGQDUCKY 2003-09-08
  • 打赏
  • 举报
回复
如果出现重复是返回到while还是 insert into log
我怎么一出现错误就插入log 1万多条数据

你的异常处理都没有做什么动作,除了INSERT,没有RETURN或goto之类的
语句,肯定是报错了INSERT然后返回循环。。。。。。
Tuxedo10 2003-09-08
  • 打赏
  • 举报
回复
谢谢各位

改进版,如果出现重复是返回到while还是 insert into log
我怎么一出现错误就插入log 1万多条数据
真让人受不了

while cur_equlas_openup%found loop
begin
insert into zwdjdxg z(z.djbh,z.gxcs,z.workerid,z.ywlbid,z.mid,
z.dhhm,z.dhkh,z.blrq,z.dbr,z.dbrzjhm,
z.dbrlxdh,z.dbrzjlxid,z.removetag,z.destroytime,z.tradetypecode,z.deptid,
z.khmc,z.zjlxid,z.zjhm,z.txdz,z.yzbm,z.lxdh1,z.lxr,
z.tclxid) values(
bcustomerid,'99','8888','WXGH','SH002',
bmphonecode,bsimcardno,bopendate,bassurename,bassurepid,
bassurelink,bassuretypecode,bremovetag,bdestroytime,btradetypecode,bdepartid,
ausrname,apassporttypecode,ausrpid,ausraddress,apostcode,alinkhomephone,alinkman,
cfeesetcode
);
commit;
EXCEPTION
WHEN OTHERS THEN
begin
-- Assign values to the log variables, using built-in
-- functions.
v_ErrorCode := SQLCODE;
v_ErrorMsg := SQLERRM;
v_CurrentUser := USER;
v_Information := 'Error encountered on ' ||
TO_CHAR(SYSDATE) || ' by database user ' || v_CurrentUser;
-- Insert the log message into log_table.
INSERT INTO log l (l.workerid,l.czrq,l.funcid,l.actor,l.operate)
VALUES ('8888',sysdate,v_ErrorCode||'导入数据','导入数据',bcustomerid||'v_ErrorMsg');
commit;
end;

end;
end loop;
nboys 2003-09-08
  • 打赏
  • 举报
回复
up
LGQDUCKY 2003-09-08
  • 打赏
  • 举报
回复
少一个END;
错误处理
BEGIN
。。。。。。。
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
--插入日志
insert into aaaa(aaaa,bbbb) values ('ss','ss01');
END;
加上这个END就OK了。异常处理的一般格式如:

BEGIN
.........
EXCEPTION
WHEN OTHERS THEN
null;
END;
jiezhi 2003-09-08
  • 打赏
  • 举报
回复
寫錯了,是exception,not excaption
Tuxedo10 2003-09-08
  • 打赏
  • 举报
回复

commit;
--错误处理
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
--插入日志
insert into aaaa(aaaa,bbbb) values ('ss','ss01');

提示:
出现符号“excaption”需要其中之一 begin、declare、end 、for、goto

jiezhi 2003-09-08
  • 打赏
  • 举报
回复

v_ErrorCode NUMBER; -- Code for the error
v_ErrorMsg VARCHAR2(200); -- Message text for the error
v_CurrentUser VARCHAR2(8); -- Current database user
v_Information VARCHAR2(100); -- Information about the error
jiezhi 2003-09-08
  • 打赏
  • 举报
回复
一段通用的錯誤處理的例子(把錯誤寫到了的自己建立的記錄錯誤的日志表里面):
EXCEPTION
WHEN OTHERS THEN
-- Assign values to the log variables, using built-in
-- functions.
v_ErrorCode := SQLCODE;
v_ErrorMsg := SQLERRM;
v_CurrentUser := USER;
v_Information := 'Error encountered on ' ||
TO_CHAR(SYSDATE) || ' by database user ' || v_CurrentUser;
-- Insert the log message into log_table.
INSERT INTO log_table (code, message, info)
VALUES (v_ErrorCode, v_ErrorMsg, v_Information);
zhuzhuyan 2003-09-08
  • 打赏
  • 举报
回复
1。如果你的目标表有唯一索引,在insert第一条后,发现又有一条是重复的,就应该走到异常,insert到你的错误表后,在进行判断,接着循环。

2。异常处理的时候,我觉得没有必要在用一个begin,end块。只要有插入异常表的insert语句就够了吧。块多了会影响效率。
begin
EXCEPTION
--begin(这个begin块我觉得没有必要)
WHEN OTHERS THEN
....插入你的异常表
--end(这个begin块我觉得没有必要)
end;
3。你的意思是不是同一个记录在异常表中插入很多次?我怀疑你的过程没有循环出来。
看你的过程,因为你用的是fetch,用了%found loop,在过程的end loop前却没有取第二条数据的语句,即缺少fetch语句。另外,好像cursor用过了没有关闭啊。close cursor。

我能看到的就这么多,楼主看看吧。:)

17,377

社区成员

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

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