1. 程式人生 > >Oracle基礎:5: sql錯誤資訊的控制和定位

Oracle基礎:5: sql錯誤資訊的控制和定位

在這裡插入圖片描述
在sqlplus中執行的sql出錯之後應該如何處理和對應,多行sql語句或者儲存過程的資訊如何進行錯誤定位,這篇文章將結合例項進行簡單地說明。

環境準備

使用Oracle的精簡版建立docker方式的demo環境,詳細可參看:

如何進行錯誤定位

場景:
假如有3行insert的sql語句,中間一行出錯之後,後續繼續執行的情況下,如何定位到第二行?

dbms_utility.format_error_backtrace

通過使用dbms_utility.format_error_backtrace可以得到ERROR at line xxx:的資訊,這對我們較為有用,我們接下來進行確認

[email protected]:~$ sqlplus system/[email protected] <<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
[email protected]
:~$

可以看到,報錯的時候提示了行號,但是行號是1,這是因為這種寫法以一行為單位,自然是如此,如果是單個多行的儲存過程,將會更加清晰。

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

所以我們將這個例子進行改造,三行insert的sql放到檔案之中,然後在使用dbms_utility.format_error_backtrace來進行確認

[email protected]:~$ 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;
[email protected]
:~$

然後在嘗試一下是否能夠確認行號,會發現仍然不能精確定位:

[email protected]:~$ sqlplus system/[email protected] <<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
[email protected]:~$ 

因為dbms_utility.format_error_backtrace更多的場景是在於儲存過程的錯誤定位,接下來我們使用一個簡單的儲存過程例子來進行確認錯誤行號定位, 先看一個正常的儲存過程,把上面的內容稍微修改一下:

[email protected]:~$ 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();
[email protected]:~$

結果執行資訊如下

[email protected]2341c0:~$ 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
[email protected]:~$ 

接下來我們修改一下內容,使得第二行主鍵重複

[email protected]:~$ 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();
[email protected]:~$ 

再次執行,自然會出錯,但是可以看到,正確報出了所在行數,這是procedure的機制提示的資訊

[email protected]:~$ 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
[email protected]:~$

可以看到,ORA-06512: at “SYSTEM.ADDSTUDENTS”, line 10的資訊就是我們期待的資訊,提示出在這個儲存過程的第10行執行出現問題,而實際可以使用dbms_utility.format_error_backtrace結合exception給出更為清晰地方式,比如:

[email protected]:~$ 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();
[email protected]:~$

執行結果確認:

[email protected]:~$ 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
[email protected]:~$ 

這樣則可以看出能夠比較清晰地進行錯誤的定位了,但是由於功能受限,所以實際使用場景仍然較為有限,但是定位儲存過程的資訊則可以使用dbms_utility.format_error_backtrace等進行確認。

總結

多行sql執行定位可以考慮拆成單行來確認,而儲存過程則可結合format_error_backtrace等進行確認以提供問題出現的所在行號。