麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 數(shù)據(jù)庫(kù) > Oracle > 正文

Oracle查詢(xún)sql錯(cuò)誤信息的控制和定位

2024-08-29 14:01:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在sqlplus中執(zhí)行的sql出錯(cuò)之后應(yīng)該如何處理和對(duì)應(yīng),多行sql語(yǔ)句或者存儲(chǔ)過(guò)程的信息如何進(jìn)行錯(cuò)誤定位,這篇文章將結(jié)合實(shí)例進(jìn)行簡(jiǎn)單地說(shuō)明。

環(huán)境準(zhǔn)備

使用Oracle的精簡(jiǎn)版創(chuàng)建docker方式的demo環(huán)境

如何進(jìn)行錯(cuò)誤定位

場(chǎng)景:

假如有3行insert的sql語(yǔ)句,中間一行出錯(cuò)之后,后續(xù)繼續(xù)執(zhí)行的情況下,如何定位到第二行?

dbms_utility.format_error_backtrace

通過(guò)使用dbms_utility.format_error_backtrace可以得到ERROR at line xxx:的信息,這對(duì)我們較為有用,我們接下來(lái)進(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);> 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:~$ 

可以看到,報(bào)錯(cuò)的時(shí)候提示了行號(hào),但是行號(hào)是1,這是因?yàn)檫@種寫(xiě)法以一行為單位,自然是如此,如果是單個(gè)多行的存儲(chǔ)過(guò)程,將會(huì)更加清晰。

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

所以我們將這個(gè)例子進(jìn)行改造,三行insert的sql放到文件之中,然后在使用dbms_utility.format_error_backtrace來(lái)進(jìn)行確認(rèn)

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:~$

然后在嘗試一下是否能夠確認(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);> 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:~$ 

因?yàn)閐bms_utility.format_error_backtrace更多的場(chǎng)景是在于存儲(chǔ)過(guò)程的錯(cuò)誤定位,接下來(lái)我們使用一個(gè)簡(jiǎn)單的存儲(chǔ)過(guò)程例子來(lái)進(jìn)行確認(rèn)錯(cuò)誤行號(hào)定位, 先看一個(gè)正常的存儲(chǔ)過(guò)程,把上面的內(nèi)容稍微修改一下:

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:~$

結(jié)果執(zhí)行信息如下

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:~$ 

接下來(lái)我們修改一下內(nèi)容,使得第二行主鍵重復(fù)

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:~$ 

再次執(zhí)行,自然會(huì)出錯(cuò),但是可以看到,正確報(bào)出了所在行數(shù),這是procedure的機(jī)制提示的信息

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的信息就是我們期待的信息,提示出在這個(gè)存儲(chǔ)過(guò)程的第10行執(zhí)行出現(xiàn)問(wèn)題,而實(shí)際可以使用dbms_utility.format_error_backtrace結(jié)合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:~$

執(zhí)行結(jié)果確認(rèn):

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:~$ 

這樣則可以看出能夠比較清晰地進(jìn)行錯(cuò)誤的定位了,但是由于功能受限,所以實(shí)際使用場(chǎng)景仍然較為有限,但是定位存儲(chǔ)過(guò)程的信息則可以使用dbms_utility.format_error_backtrace等進(jìn)行確認(rèn)。

小結(jié)

多行sql執(zhí)行定位可以考慮拆成單行來(lái)確認(rèn),而存儲(chǔ)過(guò)程則可結(jié)合format_error_backtrace等進(jìn)行確認(rèn)以提供問(wèn)題出現(xiàn)的所在行號(hào)。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到oracle教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久美女免费视频 | 自拍偷拍亚洲图片 | 宅男噜噜噜66国产在线观看 | 国产69久久精品成人看 | 国产乱淫av片免费网站 | 亚洲国产精品高潮呻吟久久 | 97porn| 国产一区二区三区视频观看 | 一区在线不卡 | 日韩精品久久久 | 毛片免费视频播放 | 羞羞视频免费观看网站 | 久久人人爽人人爽人人片av高请 | 一级网站 | 激情在线视频 | 久久久久久久久国产精品 | 国产一区日韩一区 | 成人毛片视频在线观看 | 天天夜夜操操 | 国产一级不卡毛片 | 久久99精品久久久久久青青日本 | 国产精品无码久久久久 | 91av在线免费播放 | 91成人一区二区三区 | 欧美性猛交xxxxx按摩国内 | 在线成人免费观看 | 看91| 91精品国产综合久久婷婷香蕉 | 日本aaaa片毛片免费观看视频 | 亚洲欧美成aⅴ人在线观看 av免费在线播放 | 亚洲国产成人一区 | 一级黄色片在线看 | 色就操| 中文字幕精品久久 | 亚洲视频综合网 | 国产亚洲高清在线精品不卡 | 久久久久久久久日本理论电影 | 亚洲性生活免费视频 | 美女被免费网站在线软件 | 91成人在线免费视频 | 新久草视频 |