在sqlplus中执行的sql出错之后应该如何处理和对应,多行sql语句或者存储过程的信息如何进行错误定位,这篇文章将结合实例进行简单地说明。
环境准备
使用Oracle的精简版创建docker方式的demo环境,详细可参看:
- https://www.jb51.net/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); > EOF SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 13:06:07 2018 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production SQL> SQL> Name Null"htmlcode">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 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; 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); > EOF SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 13:08:27 2018 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production SQL> SQL> Name Null"mailto:oracle@e871d42341c0:~$" rel="external nofollow" >oracle@e871d42341c0:~$因为dbms_utility.format_error_backtrace更多的场景是在于存储过程的错误定位,接下来我们使用一个简单的存储过程例子来进行确认错误行号定位, 先看一个正常的存储过程,把上面的内容稍微修改一下:
oracle@e871d42341c0:~$ cat /tmp/addstudent.sql create or replace PROCEDURE addstudents IS student_count number; BEGIN delete 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 <<EOF set serveroutput on; @/tmp/addstudent.sql EOF SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 04:42:11 2018 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production SQL> SQL> Procedure created. sql set count before :0 sql set count after :3 PL/SQL procedure successfully completed. SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production oracle@e871d42341c0:~$接下来我们修改一下内容,使得第二行主键重复
oracle@e871d42341c0:~$ cat /tmp/addstudent.sql create or replace PROCEDURE addstudents IS student_count number; BEGIN delete 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 <<EOF set serveroutput on; @/tmp/addstudent.sql EOF SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 04:44:25 2018 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production SQL> SQL> Procedure created. sql set count before :0 BEGIN addstudents(); END; * ERROR at line 1: ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated ORA-06512: at "SYSTEM.ADDSTUDENTS", line 10 ORA-06512: at line 1 SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production oracle@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 addstudents IS student_count number; BEGIN delete 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); exception when others then dbms_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 <<EOF set serveroutput on; @/tmp/addstudent.sql EOF SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 04:49:27 2018 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production SQL> SQL> Procedure created. sql set count before :0 exception happend with line info : ORA-06512: at "SYSTEM.ADDSTUDENTS", line 10 PL/SQL procedure successfully completed. SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production oracle@e871d42341c0:~$这样则可以看出能够比较清晰地进行错误的定位了,但是由于功能受限,所以实际使用场景仍然较为有限,但是定位存储过程的信息则可以使用dbms_utility.format_error_backtrace等进行确认。
小结
多行sql执行定位可以考虑拆成单行来确认,而存储过程则可结合format_error_backtrace等进行确认以提供问题出现的所在行号。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 童丽《民歌童丽(HQCD)》【WAV+CUE】
- 童丽《绝对收藏》2022头版限量编号[WAV+CUE][1G]
- 腾格尔《出走天堂》MQA-UHQCD限量版[低速原抓WAV+CUE][1G]
- 田震《时光音乐会》纯银CD[低速原抓WAV+CUE][1G]
- 炉石传说11月初最强登顶卡组合集 炉石传说11月初登顶卡组分享
- lol炼金龙魂详细属性是什么 2024炼金龙魂详细属性介绍
- 英雄联盟六个龙魂是哪六个 英雄联盟六个龙魂介绍一览
- 《忆蚀》Subliminal:揭秘后室之谜,路知行献声Weplay文化展
- 初始之部制作人气漫画改编游戏《我家大师兄脑子有坑》参展2024WePlay
- 《异环》「奇点测试」定档11.28 超自然都市轻喜剧即将放送!
- 16层乐队.2024-大快朵颐【摩登天空】【FLAC分轨】
- 群星.1988-电视金曲巡礼【EMI百代】【WAV+CUE】
- 群星.1992-电视金曲巡礼VOL.2【EMI百代】【WAV+CUE】
- 廖昌永《情缘HQ》头版限量[低速原抓WAV+CUE]
- 蔡琴《老歌》头版限量编号MQA-24K金碟[低速原抓WAV+CUE]