PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据
这篇文章主要讲解了“PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据”吧!
一:利用scott下的emp表构造数据--构造数据CreateTablet1AsSelect*Fromscott.empWhere1=2;AlterTablet1AddConstraintpk_emp_empnoPrimaryKey(empno);AlterTablet1Modify(enameNotNull);AlterTablet1AddConstraintchk_sqlcheck(Sal>=800);CreateTablet2AsSelect*FROMscott.emp;InsertIntot2Select*FROMscott.empWhereempnoIn(7369,7499);Updatet2Setsal=700WhereempnoIn(7521,7566);Updatet2Setename=NullWhereempno=7698;CreateTablet_errorAsSelect*Fromscott.empWhere1=2;AlterTablet_errorAdd(error_idxnumber,ErrorCodeVarchar2(50));二:程序处理块
--一次获取所有ORA-24381错误的记录Declare--定义ORA-24381对应的异常Excp_Bulk_ErrorsException;--将异常和错误号关联PragmaException_Init(Excp_Bulk_Errors,-24381);--定义存储ErrorIndex和ErrorCode的变量n_Err_IdxNumber;Vc_Err_CodeVarchar2(50);Cur_Ref_EmpSys_Refcursor;Pi_Fetch_LimitPls_Integer:=1000;TypeTyp_EmpIsTableOfEmp%RowtypeIndexByBinary_Integer;Typ_Emp_RecTyp_Emp;BeginOpenCur_Ref_EmpForSelect*FromT2;--批量获取FetchCur_Ref_EmpBulkCollectIntoTyp_Emp_RecLimitPi_Fetch_Limit;--批量执行ForalliIn1..Typ_Emp_Rec.CountSaveExceptionsExecuteImmediate'insertintot1values(:empno,:ename,:job,:mgr,:hiredate,:sal,:comm,:deptno)'UsingTyp_Emp_Rec(i).Empno,Typ_Emp_Rec(i).Ename,Typ_Emp_Rec(i).Job,Typ_Emp_Rec(i).Mgr,Typ_Emp_Rec(i).Hiredate,Typ_Emp_Rec(i).Sal,Typ_Emp_Rec(i).Sal,Typ_Emp_Rec(i).Deptno;ExceptionWhenExcp_Bulk_ErrorsThen--清空错误日志表ExecuteImmediate'deletefromt_error';ForiIn1..Sql%Bulk_Exceptions.Count()Loopn_Err_Idx:=Sql%Bulk_Exceptions(i).Error_Index;Vc_Err_Code:=Sql%Bulk_Exceptions(i).Error_Code;ExecuteImmediate'insertintot_errorvalues(:empno,:ename,:job,:mgr,:hiredate,:sal,:comm,:deptno,:idx,:code)'UsingTyp_Emp_Rec(n_Err_Idx).Empno,Typ_Emp_Rec(n_Err_Idx).Ename,Typ_Emp_Rec(n_Err_Idx).Job,Typ_Emp_Rec(n_Err_Idx).Mgr,Typ_Emp_Rec(n_Err_Idx).Hiredate,Typ_Emp_Rec(n_Err_Idx).Sal,Typ_Emp_Rec(n_Err_Idx).Sal,Typ_Emp_Rec(n_Err_Idx).Deptno,n_Err_Idx,Vc_Err_Code;EndLoop;End;
感谢各位的阅读,以上就是“PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据”的内容了,经过本文的学习后,相信大家对PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。