Oracle查詢sql錯(cuò)誤信息的控制和定位
在sqlplus中執(zhí)行的sql出錯(cuò)之后應(yīng)該如何處理和對(duì)應(yīng),多行sql語(yǔ)句或者存儲(chǔ)過程的信息如何進(jìn)行錯(cuò)誤定位,這篇文章將結(jié)合實(shí)例進(jìn)行簡(jiǎn)單地說明。
環(huán)境準(zhǔn)備
使用Oracle的精簡(jiǎn)版創(chuàng)建docker方式的demo環(huán)境,詳細(xì)可參看:
如何進(jìn)行錯(cuò)誤定位
場(chǎng)景:
假如有3行insert的sql語(yǔ)句,中間一行出錯(cuò)之后,后續(xù)繼續(xù)執(zhí)行的情況下,如何定位到第二行?
dbms_utility.format_error_backtrace
通過使用dbms_utility.format_error_backtrace可以得到ERROR at line xxx:的信息,這對(duì)我們較為有用,我們接下來進(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:~$
可以看到,報(bào)錯(cuò)的時(shí)候提示了行號(hào),但是行號(hào)是1,這是因?yàn)檫@種寫法以一行為單位,自然是如此,如果是單個(gè)多行的存儲(chǔ)過程,將會(huì)更加清晰。
ERROR at line 1: ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
所以我們將這個(gè)例子進(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)行號(hào),會(huì)發(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:~$
因?yàn)閐bms_utility.format_error_backtrace更多的場(chǎng)景是在于存儲(chǔ)過程的錯(cuò)誤定位,接下來我們使用一個(gè)簡(jiǎn)單的存儲(chǔ)過程例子來進(jìn)行確認(rèn)錯(cuò)誤行號(hào)定位, 先看一個(gè)正常的存儲(chǔ)過程,把上面的內(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í)行,自然會(huì)出錯(cuò),但是可以看到,正確報(bào)出了所在行數(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的信息就是我們期待的信息,提示出在這個(gè)存儲(chǔ)過程的第10行執(zhí)行出現(xiàn)問題,而實(shí)際可以使用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)行錯(cuò)誤的定位了,但是由于功能受限,所以實(shí)際使用場(chǎng)景仍然較為有限,但是定位存儲(chǔ)過程的信息則可以使用dbms_utility.format_error_backtrace等進(jìn)行確認(rèn)。
小結(jié)
多行sql執(zhí)行定位可以考慮拆成單行來確認(rèn),而存儲(chǔ)過程則可結(jié)合format_error_backtrace等進(jìn)行確認(rèn)以提供問題出現(xiàn)的所在行號(hào)。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
在ADF中跟蹤SQL執(zhí)行時(shí)間實(shí)現(xiàn)代碼
ADF是oracle提供的一套企業(yè)開發(fā)的解決方案,本文將實(shí)現(xiàn)在ADF中跟蹤SQL執(zhí)行時(shí)間2012-11-11Oracle數(shù)據(jù)庫(kù)如何獲取當(dāng)前自然周,當(dāng)前周的起始和結(jié)束日期
Oracle數(shù)據(jù)庫(kù)如何獲取當(dāng)前自然周,當(dāng)前周的起始和結(jié)束日期問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Oracle數(shù)據(jù)遷移MySQL的三種簡(jiǎn)單方法
對(duì)于許多企業(yè)而言,遷移數(shù)據(jù)庫(kù)時(shí)最大的挑戰(zhàn)之一是如何從一個(gè)數(shù)據(jù)庫(kù)平臺(tái)順利遷移到另一個(gè)平臺(tái),下面這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)遷移MySQL的三種簡(jiǎn)單方法,需要的朋友可以參考下2023-06-06Navicat for oracle創(chuàng)建數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Navicat for oracle創(chuàng)建數(shù)據(jù)庫(kù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11oracle—SQL技巧之(二)WMSYS.WM_CONCAT函數(shù)實(shí)現(xiàn)多行記錄用逗號(hào)拼接在一起
由于業(yè)務(wù)系統(tǒng)的交易記錄有很多,常常有些主管需要看到所有的記錄情況;又不想滾動(dòng);接下來介紹使用Oracle自帶的函數(shù) WMSYS.WM_CONCAT,進(jìn)行拼接,感興趣的朋友可以了解下2013-01-01oracle不支持的字符集orai18n.jar?ZHS16GBK異常問題解決辦法
字符集是數(shù)據(jù)庫(kù)中用來表示和存儲(chǔ)字符的編碼系統(tǒng),這篇文章主要給大家介紹了關(guān)于oracle不支持的字符集orai18n.jar?ZHS16GBK異常問題的解決辦法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02Oracle如何修改當(dāng)前的序列值實(shí)例詳解
很多時(shí)候我們都會(huì)用到oracle序列,那么我們?cè)趺葱薷男蛄械漠?dāng)前值呢?下面這篇文章主要給大家介紹了關(guān)于Oracle如何修改當(dāng)前的序列值的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05