有以下过程,我想每次触发异常时都回滚事务,应该怎么改呢?
不会在每个异常处理后面都加上
 if j_StartWork = 1 then
        rollback work;
    end if;   

-------------------------------------------------------
Create or Replace procedure testproc(
    in_SheetID  in char,
    in_Checker  in char,
    in_OrderNo  in int,
    out_Result  out int) is  
 
  STATUS_IS_VALID       exception;
  SHEET_NOT_FOUND     exception;
  UNKNOW_SHEET_STATUS   exception;
  QTY_ERROR exception;
begin
  j_Result := 0;
  j_StartWork := 0;
  
  j_startwork := 1;
  j_SheetType := 2503;
  
  
  j_BreakPoint := 2502115;      
  commit work;    
  out_Result := j_Result;
  return;
  
exception 
  when SHEET_NOT_FOUND then
    sMsg := '单据没有找到';
    raise_application_error(-20000,'SQLCODE='||SQLCODE||',Breakpoint='||j_Breakpoint||',MSG='||sMsg);
  when STATUS_IS_VALID then
    sMsg := '单据状态错误';
    raise_application_error(-20000,'SQLCODE='||SQLCODE||',Breakpoint='||j_Breakpoint||',MSG='||sMsg);
  when QTY_ERROR then
    sMsg := '验收数据验证错误';
    raise_application_error(-20000,'SQLCODE='||SQLCODE||',Breakpoint='||j_Breakpoint||',MSG='||sMsg);
  when UNKNOW_SHEET_STATUS then
    sMsg := '未定义状态标志';
    raise_application_error(-20000,'SQLCODE='||SQLCODE||',Breakpoint='||j_Breakpoint||',MSG='||sMsg);
  when others then
    if j_StartWork = 1 then
        rollback work;
    end if;   
    raise_application_error(-20000,'SQLCODE='||SQLCODE||','|| 'Breakpoint='||j_Breakpoint||',MSG='||SQLERRM); 
end testproc;

解决方案 »

  1.   

    定义一个共通的EXCEPTION不需要写得那么多了。你的
    if   j_StartWork   =   1   then
                    rollback   work;
            end   if;   本来就只有OTHERS有呀,
      

  2.   

    我想做到SHEET_NOT_FOUND等异常发生时也可以回滚,我应该怎么写呢
      

  3.   

    你一定需要你自己定义的exception吗?
    用一个就是了,在raise之前,设置sMsg,然后到Exception块,不就好了吗?
    只需要写你自己的这个exception和others.我看你的错误处理,基本上都是一样的。
      

  4.   

    Croatia你是不是这个意思?
    Create   or   Replace   procedure   testproc( 
            in_SheetID     in   char, 
            in_Checker     in   char, 
            in_OrderNo     in   int, 
            out_Result     out   int)   is     
      
        STATUS_IS_VALID               exception; 
        SHEET_NOT_FOUND         exception; 
        UNKNOW_SHEET_STATUS       exception; 
        QTY_ERROR exception; 
    begin 
        j_Result   :=   0; 
        j_StartWork   :=   0; 
        
        j_startwork   :=   1; 
        j_SheetType   :=   2503; 
        
        ...
        if ... then
         sMag := 'error occurred';
         raise MY_DEFINE_ERROR;
        end if;
        ...
        j_BreakPoint   :=   2502115;             
        commit   work;         
        out_Result   :=   j_Result; 
        return; 
        
    exception   
        when   MY_DEFINE_ERROR   then 
            if   j_StartWork   =   1   then 
                    rollback   work; 
            end   if;  
            raise_application_error(-20000, 'SQLCODE= ' | |SQLCODE | | ',Breakpoint= ' | |j_Breakpoint | | ',MSG= ' | |sMsg); 
        when   others   then 
            if   j_StartWork   =   1   then 
                    rollback   work; 
            end   if;       
            raise_application_error(-20000, 'SQLCODE= ' | |SQLCODE | | ', ' | |   'Breakpoint= ' | |j_Breakpoint | | ',MSG= ' | |SQLERRM);   
    end   testproc;