Oracle存储过程执行异常捕获(错误行号,错误代码,错误信息)
			
					(2016-12-29 15:51:32)		
		
			
				
					| 标签: 存储过程异常捕获oracle捕获异常行号oracle捕获错误行捕获存储过程错误行捕获存储过程错误信息 | 分类:
						Oracle数据库 | 
			
		 
						
		
			      
在Oracle存储过程的调试执行过程中,会遇到异常错误无法准确定位的问题,这就造成不知道错误在哪里,无法及时准确地对错误进行修改。那有没有方法来快速来定位错误的原因以及错误在哪一行呢?答案是肯定的,本文通过实例进行测试,供大家参考使用。
一、创建异常记录日志表
 drop table t_pro_error_log;
 create table t_pro_error_log
(
 
pro_name     
varchar2(300),
  error_desc   
 varchar2(2000),
  data_cd  
   
  varchar2(20),
  exec_date
   
 date
)
compress
nologging;
-- add comments to the table
comment on table t_pro_error_log
  is '存储过程执行错误日志表';
-- add comments to the columns
comment on column t_pro_error_log.pro_name
  is '存储过程名称';
comment on column t_pro_error_log.error_desc
  is '错误详细描述';
comment on column t_pro_error_log.data_cd
  is '数据账期';
comment on column t_pro_error_log.exec_date
  is '执行时间';
二、存储过程应用示例
create or replace procedure pr_error_code_test(acct_date in
varchar2)
is
v_sql          
varchar2(20000);      
--执行SQL语句
v_rows         
integer;              
--影响行数
v_step         
integer:=0;           
--执行步骤记录
v_backtrace    
varchar2(1000);       
--返回错误行
v_error_cont   
varchar2(1000);       
--整合错误内容
begin
  --要执行的sql语句
  v_sql:='
  create table t_mytest
  as
  select '||acct_date||' as stat_dt,120/0 as
tj_val
  from dual
  ';
  execute immediate v_sql;
  v_rows:=sql%rowcount;
  v_step:=v_step+1;
 
dbms_output.put_line(v_step||'------'||v_rows);
 
--异常错误记录处理
exception
  when others then
   
v_backtrace:=dbms_utility.format_error_backtrace;
   
--回滚未提交部分
   
rollback;
   
v_error_cont:='异常错误为:'||sqlerrm||'--'||sqlcode||'--'||v_backtrace;
    insert into
t_pro_error_log(pro_name,error_desc,data_cd,exec_date)
   
values('pr_error_code_test',v_error_cont,acct_date,sysdate);
   
commit;
   
return;
    
end pr_error_code_test;
三、执行存储过程及查看异常记录
begin
  pr_error_code_test('20161229');
  end;
--查看异常错误信息
select * from t_pro_error_log;
PRO_NAME             
ERROR_DESC                                 
DATA_CD             
EXEC_DATE
------------------   
------------------------------------------- --------------------
-----------
pr_error_code_test   异常错误为:ORA-01476: 除数为 0---1476--ORA-06512: 在
"PR_ERROR_CODE_TEST", line
17  
20161229   2016-12-29
 
捕获存储过程错误信息
本文参考:https://community.oracle.com/thread/966422
							
		
						
		
        	
            
	        	
	            	
	                 喜欢
喜欢
	             
                                
                    0
                     赠金笔
赠金笔
                 
                
	         
            
		 
		
		
		加载中,请稍候......