0

我有以下编译得很好的sproc:

CREATE OR REPLACE PROCEDURE SSACHDEV.SyncTeleappWithClientinfo
as
mine varchar2(1);
v_teleCaseNbr      number;
v_cashwithappyn varchar2(1);

CURSOR TeleAppCursor
is
    Select 
        distinct casenbr, cashwithappyn
    from TeleApp;

BEGIN
dbms_output.put_line('begin');
open TeleAppCursor;

LOOP
    fetch TeleAppCursor into v_teleCaseNbr, v_cashwithappyn;
    EXIT when TeleAppCursor%NOTFOUND; -- this one has the problem

    Select cashwithappyn into mine from ClientInfo where casenbr = v_teleCaseNbr and trim(cashwithappyn) is null;

END LOOP;
dbms_output.put_line('end');
END;

但是当我尝试使用以下命令运行它时:

BEGIN 
  SSACHDEV.SYNCTELEAPPWITHCLIENTINFO;
END;

我收到以下错误:

ORA-01403: no data found
ORA-06512: at "SSACHDEV.SYNCTELEAPPWITHCLIENTINFO", line 21
ORA-06512: at line 2

有谁知道为什么?或者我能做些什么来避免这些问题?

4

2 回答 2

4

发生这种情况是因为过程中的第二个select语句 ( Select cashwithappyn into mine from ..) 没有返回数据。在您的过程中添加EXCEPTION部分以处理该异常。

CREATE OR REPLACE PROCEDURE SSACHDEV.SyncTeleappWithClientinfo
as
mine varchar2(1);
v_teleCaseNbr      number;
v_cashwithappyn varchar2(1);

CURSOR TeleAppCursor
is
    Select 
        distinct casenbr, cashwithappyn
    from TeleApp;

BEGIN
dbms_output.put_line('begin');
open TeleAppCursor;

LOOP
    fetch TeleAppCursor into v_teleCaseNbr, v_cashwithappyn;
    EXIT when TeleAppCursor%NOTFOUND;

    Select cashwithappyn into mine from ClientInfo where casenbr = v_teleCaseNbr and trim(cashwithappyn) is null;

END LOOP;
dbms_output.put_line('end');

EXCEPTION
  WHEN NO_DATA_FOUND 
  THEN  dbms_output.put_line(' no data found'); -- for example
END;
于 2012-09-28T16:36:07.707 回答
2

您是否确定异常部分应该放在循环之外?我宁愿把它放在循环中。至于我,plsql应该看起来像

CREATE OR REPLACE PROCEDURE SSACHDEV.SyncTeleappWithClientinfo
as
mine varchar2(1);
v_teleCaseNbr      number;
v_cashwithappyn varchar2(1);

CURSOR TeleAppCursor
is
    Select 
        distinct casenbr, cashwithappyn
    from TeleApp;

BEGIN
dbms_output.put_line('begin');
open TeleAppCursor;

LOOP
    fetch TeleAppCursor into v_teleCaseNbr, v_cashwithappyn;
    EXIT when TeleAppCursor%NOTFOUND;
    begin
       Select cashwithappyn into mine from ClientInfo where casenbr = v_teleCaseNbr and trim(cashwithappyn) is null;
    when no_data_found then dbms_output.put_line('end');
    end;

END LOOP;

END;

但可以肯定的是,应该使用 LEFT JOIN 在游标本身中完成这些工作,以免数据库因循环内的大量小查询而发疯。

于 2012-09-28T18:40:42.910 回答