侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Oracle查询sql错误信息的控制和定位

数据库  /  管理员 发布于 6年前   161

在sqlplus中执行的sql出错之后应该如何处理和对应,多行sql语句或者存储过程的信息如何进行错误定位,这篇文章将结合实例进行简单地说明。

环境准备

使用Oracle的精简版创建docker方式的demo环境,详细可参看:

  • https:///article/153533.htm

如何进行错误定位

场景:

假如有3行insert的sql语句,中间一行出错之后,后续继续执行的情况下,如何定位到第二行?

dbms_utility.format_error_backtrace

通过使用dbms_utility.format_error_backtrace可以得到ERROR at line xxx:的信息,这对我们较为有用,我们接下来进行确认

oracle@e871d42341c0:~$ sqlplus system/abcd1234@XE <<EOF> SET SERVEROUTPUT ON> desc student> delete from student;> select * from student;> insert into student values (1001, 'liumiaocn');> insert into student values (1001, 'liumiao');> insert into student values (1003, 'michael');> select * from student;> commit;> exec dbms_output.put_line(dbms_utility.format_error_backtrace);> EOFSQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 13:06:07 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> SQL> Name    Null?  Type ----------------------------------------- -------- ---------------------------- STUID    NOT NULL NUMBER(4) STUNAME     VARCHAR2(50)SQL> 2 rows deleted.SQL> no rows selectedSQL> 1 row created.SQL> insert into student values (1001, 'liumiao')*ERROR at line 1:ORA-00001: unique constraint (SYSTEM.SYS_C007024) violatedSQL> 1 row created.SQL>    STUID STUNAME---------- --------------------------------------------------   1001 liumiaocn   1003 michaelSQL> Commit complete.SQL> PL/SQL procedure successfully completed.SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$ 

可以看到,报错的时候提示了行号,但是行号是1,这是因为这种写法以一行为单位,自然是如此,如果是单个多行的存储过程,将会更加清晰。

ERROR at line 1:ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated

所以我们将这个例子进行改造,三行insert的sql放到文件之中,然后在使用dbms_utility.format_error_backtrace来进行确认

oracle@e871d42341c0:~$ cat /tmp/sqltest1.sql desc studentdelete from student;select * from student;insert into student values (1001, 'liumiaocn');insert into student values (1001, 'liumiao');insert into student values (1003, 'michael');select * from student;commit;oracle@e871d42341c0:~$

然后在尝试一下是否能够确认行号,会发现仍然不能精确定位:

oracle@e871d42341c0:~$ sqlplus system/abcd1234@XE <<EOF> SET SERVEROUTPUT ON> @/tmp/sqltest1.sql> exec dbms_output.put_line(dbms_utility.format_error_backtrace);> EOFSQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 13:08:27 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> SQL> Name    Null?  Type ----------------------------------------- -------- ---------------------------- STUID    NOT NULL NUMBER(4) STUNAME     VARCHAR2(50)2 rows deleted.no rows selected1 row created.insert into student values (1001, 'liumiao')*ERROR at line 1:ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated1 row created.   STUID STUNAME---------- --------------------------------------------------   1001 liumiaocn   1003 michaelCommit complete.SQL> PL/SQL procedure successfully completed.SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$ 

因为dbms_utility.format_error_backtrace更多的场景是在于存储过程的错误定位,接下来我们使用一个简单的存储过程例子来进行确认错误行号定位, 先看一个正常的存储过程,把上面的内容稍微修改一下:

oracle@e871d42341c0:~$ cat /tmp/addstudent.sql create or replace PROCEDURE addstudentsISstudent_count number;BEGINdelete from student;select count(*) into student_count from student;dbms_output.put('sql set count before :');dbms_output.put_line(student_count);insert into student values (1001, 'liumiaocn');insert into student values (1002, 'liumiao');insert into student values (1003, 'michael');select count(*) into student_count from student;dbms_output.put('sql set count after :');dbms_output.put_line(student_count);END;/exec addstudents();oracle@e871d42341c0:~$

结果执行信息如下

oracle@e871d42341c0:~$ sqlplus system/liumiao123 <<EOFset serveroutput on;@/tmp/addstudent.sql EOFSQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 04:42:11 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> SQL> Procedure created.sql set count before :0sql set count after :3PL/SQL procedure successfully completed.SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$ 

接下来我们修改一下内容,使得第二行主键重复

oracle@e871d42341c0:~$ cat /tmp/addstudent.sqlcreate or replace PROCEDURE addstudentsISstudent_count number;BEGINdelete from student;select count(*) into student_count from student;dbms_output.put('sql set count before :');dbms_output.put_line(student_count);insert into student values (1001, 'liumiaocn');insert into student values (1001, 'liumiao');insert into student values (1003, 'michael');select count(*) into student_count from student;dbms_output.put('sql set count after :');dbms_output.put_line(student_count);END;/exec addstudents();oracle@e871d42341c0:~$ 

再次执行,自然会出错,但是可以看到,正确报出了所在行数,这是procedure的机制提示的信息

oracle@e871d42341c0:~$ sqlplus system/liumiao123 <<EOFset serveroutput on;@/tmp/addstudent.sql EOFSQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 04:44:25 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> SQL> Procedure created.sql set count before :0BEGIN addstudents(); END;*ERROR at line 1:ORA-00001: unique constraint (SYSTEM.SYS_C007024) violatedORA-06512: at "SYSTEM.ADDSTUDENTS", line 10ORA-06512: at line 1SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$

可以看到,ORA-06512: at “SYSTEM.ADDSTUDENTS”, line 10的信息就是我们期待的信息,提示出在这个存储过程的第10行执行出现问题,而实际可以使用dbms_utility.format_error_backtrace结合exception给出更为清晰地方式,比如:

oracle@e871d42341c0:~$ cat /tmp/addstudent.sql create or replace PROCEDURE addstudentsISstudent_count number;BEGINdelete from student;select count(*) into student_count from student;dbms_output.put('sql set count before :');dbms_output.put_line(student_count);insert into student values (1001, 'liumiaocn');insert into student values (1001, 'liumiao');insert into student values (1003, 'michael');select count(*) into student_count from student;dbms_output.put('sql set count after :');dbms_output.put_line(student_count);exceptionwhen others thendbms_output.put('exception happend with line info : ');dbms_output.put_line(dbms_utility.format_error_backtrace);END;/exec addstudents();oracle@e871d42341c0:~$

执行结果确认:

oracle@e871d42341c0:~$ sqlplus system/liumiao123 <<EOFset serveroutput on;@/tmp/addstudent.sql EOFSQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 04:49:27 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> SQL> Procedure created.sql set count before :0exception happend with line info : ORA-06512: at "SYSTEM.ADDSTUDENTS", line 10PL/SQL procedure successfully completed.SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$ 

这样则可以看出能够比较清晰地进行错误的定位了,但是由于功能受限,所以实际使用场景仍然较为有限,但是定位存储过程的信息则可以使用dbms_utility.format_error_backtrace等进行确认。

小结

多行sql执行定位可以考虑拆成单行来确认,而存储过程则可结合format_error_backtrace等进行确认以提供问题出现的所在行号。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对AIDI的支持。如果你想了解更多相关内容请查看下面相关链接


  • 上一条:
    Oracle基础多条sql执行在中间的语句出现错误时的控制方式
    下一条:
    运行在容器中的Oracle XE-11g
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客