小編給大家分享一下如何實現(xiàn)Oracle查詢sql錯誤信息的控制和定位,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
站在用戶的角度思考問題,與客戶深入溝通,找到西秀網(wǎng)站設(shè)計與西秀網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋西秀地區(qū)。
環(huán)境準(zhǔn)備
使用Oracle的精簡版創(chuàng)建docker方式的demo環(huán)境
如何進(jìn)行錯誤定位
場景:
假如有3行insert的sql語句,中間一行出錯之后,后續(xù)繼續(xù)執(zhí)行的情況下,如何定位到第二行?
dbms_utility.format_error_backtrace
通過使用dbms_utility.format_error_backtrace可以得到ERROR at line xxx:的信息,這對我們較為有用,我們接下來進(jìn)行確認(rèn)
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? Type ----------------------------------------- -------- ---------------------------- STUID NOT NULL NUMBER(4) STUNAME VARCHAR2(50) SQL> 2 rows deleted. SQL> no rows selected SQL> 1 row created. SQL> insert into student values (1001, 'liumiao') * ERROR at line 1: ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated SQL> 1 row created. SQL> STUID STUNAME ---------- -------------------------------------------------- 1001 liumiaocn 1003 michael SQL> Commit complete. SQL> PL/SQL procedure successfully completed. SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production oracle@e871d42341c0:~$
可以看到,報錯的時候提示了行號,但是行號是1,這是因為這種寫法以一行為單位,自然是如此,如果是單個多行的存儲過程,將會更加清晰。
ERROR at line 1: ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
所以我們將這個例子進(jìn)行改造,三行insert的sql放到文件之中,然后在使用dbms_utility.format_error_backtrace來進(jìn)行確認(rèn)
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:~$
然后在嘗試一下是否能夠確認(rèn)行號,會發(fā)現(xiàn)仍然不能精確定位:
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? Type ----------------------------------------- -------- ---------------------------- STUID NOT NULL NUMBER(4) STUNAME VARCHAR2(50) 2 rows deleted. no rows selected 1 row created. insert into student values (1001, 'liumiao') * ERROR at line 1: ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated 1 row created. STUID STUNAME ---------- -------------------------------------------------- 1001 liumiaocn 1003 michael Commit complete. SQL> 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更多的場景是在于存儲過程的錯誤定位,接下來我們使用一個簡單的存儲過程例子來進(jìn)行確認(rèn)錯誤行號定位, 先看一個正常的存儲過程,把上面的內(nèi)容稍微修改一下:
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:~$
結(jié)果執(zhí)行信息如下
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:~$
接下來我們修改一下內(nèi)容,使得第二行主鍵重復(fù)
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:~$
再次執(zhí)行,自然會出錯,但是可以看到,正確報出了所在行數(shù),這是procedure的機(jī)制提示的信息
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行執(zhí)行出現(xiàn)問題,而實際可以使用dbms_utility.format_error_backtrace結(jié)合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:~$
執(zhí)行結(jié)果確認(rèn):
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:~$
這樣則可以看出能夠比較清晰地進(jìn)行錯誤的定位了,但是由于功能受限,所以實際使用場景仍然較為有限,但是定位存儲過程的信息則可以使用dbms_utility.format_error_backtrace等進(jìn)行確認(rèn)。
以上是“如何實現(xiàn)Oracle查詢sql錯誤信息的控制和定位”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享文章:如何實現(xiàn)Oracle查詢sql錯誤信息的控制和定位
分享URL:http://www.rwnh.cn/article10/jjeogo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、外貿(mào)建站、定制網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)