网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

PostgreSQL程序中批量绑定时怎么使用saveexceptions记录错误数据

这篇文章主要讲解了“PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据”吧!

成都创新互联公司主营嵊州网站建设的网络公司,主营网站建设方案,app软件定制开发,嵊州h5微信小程序开发搭建,嵊州网站营销推广欢迎嵊州等地区企业咨询

一:利用scott下的emp表构造数据

--构造数据
Create Table t1 As Select * From scott.emp Where 1=2;
Alter Table t1 Add Constraint pk_emp_empno Primary Key(empno);
Alter Table t1 Modify(ename Not Null);
Alter Table t1 Add Constraint chk_sql check(Sal >= 800);
Create Table t2 As Select * FROM scott.emp;
Insert Into t2 Select * FROM scott.emp Where empno In(7369,7499);
Update t2 Set sal = 700 Where empno In(7521,7566);
Update t2 Set ename = Null Where empno = 7698;
Create Table t_error As Select * From scott.emp Where 1=2;
Alter Table t_error Add(error_idx number,ErrorCode Varchar2(50));

二:程序处理块

--一次获取所有ORA-24381错误的记录
Declare
  --定义ORA-24381对应的异常
  Excp_Bulk_Errors Exception;
  --将异常和错误号关联
  Pragma Exception_Init(Excp_Bulk_Errors, -24381);
  --定义存储Error Index和Error Code的变量
  n_Err_Idx      Number;
  Vc_Err_Code    Varchar2(50);
  Cur_Ref_Emp    Sys_Refcursor;
  Pi_Fetch_Limit Pls_Integer := 1000;
  Type Typ_Emp Is Table Of Emp%Rowtype Index By Binary_Integer;
  Typ_Emp_Rec Typ_Emp;
Begin
  Open Cur_Ref_Emp For
    Select * From T2;
  --批量获取
  Fetch Cur_Ref_Emp Bulk Collect
    Into Typ_Emp_Rec Limit Pi_Fetch_Limit;
  --批量执行
  Forall i In 1 .. Typ_Emp_Rec.Count Save Exceptions Execute Immediate
                   'insert into t1 values(:empno,:ename,:job,:mgr,:hiredate,:sal,:comm,:deptno)'
                   Using Typ_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;
Exception
  When Excp_Bulk_Errors Then
    --清空错误日志表
    Execute Immediate 'delete from t_error';
    For i In 1 .. Sql%Bulk_Exceptions.Count() Loop
      n_Err_Idx   := Sql%Bulk_Exceptions(i).Error_Index;
      Vc_Err_Code := Sql%Bulk_Exceptions(i).Error_Code;
      Execute Immediate 'insert into t_error values(:empno,:ename,:job,:mgr,:hiredate,:sal,:comm,:deptno,:idx,:code)'
        Using Typ_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;
    End Loop;
End;

感谢各位的阅读,以上就是“PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据”的内容了,经过本文的学习后,相信大家对PostgreSQL程序中批量绑定时怎么使用save exceptions记录错误数据这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网站栏目:PostgreSQL程序中批量绑定时怎么使用saveexceptions记录错误数据
转载注明:http://bjjierui.cn/article/ipieei.html

其他资讯