Oracle資料庫多條sql執行語句出現錯誤時的控制方式

liumiaocn發表於2019-01-27

多條sql執行時如果在中間的語句出現錯誤,後續會不會直接執行,如何進行設定,以及其他資料庫諸如MySQL是如何對應的,這篇文章將會進行簡單的整理和說明。

環境準備

使用Oracle的精簡版建立docker方式的demo環境。

多行語句的正常執行

對上篇文章建立的兩個欄位的學生資訊表,正常新增三條資料,詳細如下:

  1. # sqlplus system/liumiao123@XE <<EOF
  2. > desc student
  3. > select * from student;
  4. > insert into student values (1001, 'liumiaocn');
  5. > insert into student values (1002, 'liumiao');
  6. > insert into student values (1003, 'michael');
  7. > commit;
  8. > select * from student;
  9. > EOF
  10. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 12:08:35 2018
  11. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  12. Connected to:
  13. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  14. SQL> Name    Null?  Type
  15.  ----------------------------------------- -------- ----------------------------
  16.  STUID    NOT NULL NUMBER(4)
  17.  STUNAME     VARCHAR2(50)
  18. SQL>
  19. no rows selected
  20. SQL>
  21. 1 row created.
  22. SQL>
  23. 1 row created.
  24. SQL>
  25. 1 row created.
  26. SQL>
  27. Commit complete.
  28. SQL>
  29.    STUID STUNAME
  30. ---------- --------------------------------------------------
  31.    1001 liumiaocn
  32.    1002 liumiao
  33.    1003 michael
  34. SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  35. #

多行語句中間出錯時的預設動作

問題:

三行insert語句,如果中間的一行出錯,預設的狀況下第三行會不會被插入進去?

我們將第二條insert語句的主鍵故意設定重複,然後進行確認第三條資料是否會進行插入即可。

  1. # sqlplus system/liumiao123@XE <<EOF
  2. desc student
  3. delete from student;
  4. select * from student;
  5. insert into student values (1001, 'liumiaocn');
  6. insert into student values (1001, 'liumiao');
  7. insert into student values (1003, 'michael');
  8. select * from student;
  9. commit;> > > > >   
  10. > EOF
  11. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 12:15:16 2018
  12. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  13. Connected to:
  14. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  15. SQL> Name    Null?  Type
  16.  ----------------------------------------- -------- ----------------------------
  17.  STUID    NOT NULL NUMBER(4)
  18.  STUNAME     VARCHAR2(50)
  19. SQL>
  20. 2 rows deleted.
  21. SQL>
  22. no rows selected
  23. SQL>
  24. 1 row created.
  25. SQL> insert into student values (1001, 'liumiao')
  26. *
  27. ERROR at line 1:
  28. ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
  29. SQL>
  30. 1 row created.
  31. SQL>
  32.    STUID STUNAME
  33. ---------- --------------------------------------------------
  34.    1001 liumiaocn
  35.    1003 michael
  36. SQL> SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  37. #

結果非常清晰地表明是會繼續執行的,在oracle中透過什麼來對其進行控制呢?

WHENEVER SQLERROR

答案很簡單,在oracle中透過WHENEVER SQLERROR來進行控制。

WHENEVER SQLERROR {EXIT [SUCCESS | FAILURE | WARNING | n | variable | :BindVariable] [COMMIT | ROLLBACK] | CONTINUE [COMMIT | ROLLBACK | NONE]}

WHENEVER SQLERROR EXIT

新增此行設定,即會在失敗的時候立即推出,接下來我們進行確認:

  1. # sqlplus system/liumiao123@XE <<EOF
  2. WHENEVER SQLERROR EXIT
  3. desc student
  4. delete from student;
  5. select * from student;
  6. insert into student values (1001, 'liumiaocn');
  7. insert into student values (1001, 'liumiao');
  8. insert into student values (1003, 'michael');
  9. select * from student;
  10. commit;> > > > > > > > >
  11. > EOF
  12. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 12:27:15 2018
  13. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  14. Connected to:
  15. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  16. SQL> SQL> Name    Null?  Type
  17.  ----------------------------------------- -------- ----------------------------
  18.  STUID    NOT NULL NUMBER(4)
  19.  STUNAME     VARCHAR2(50)
  20. SQL>
  21. 2 rows deleted.
  22. SQL>
  23. no rows selected
  24. SQL>
  25. 1 row created.
  26. SQL> insert into student values (1001, 'liumiao')
  27. *
  28. ERROR at line 1:
  29. ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
  30. Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  31. #

WHENEVER SQLERROR CONTINUE

使用CONTINUE則和預設方式下的行為一致,出錯仍然繼續執行

  1. # sqlplus system/liumiao123@XE <<EOF
  2. WHENEVER SQLERROR CONTINUE
  3. desc student
  4. delete from student;
  5. select * from student;
  6. insert into student values (1001, 'liumiaocn');
  7. insert into student values (1001, 'liumiao');
  8. insert into student values (1003, 'michael');
  9. select * from student;
  10. commit;> > > > > > > > >
  11. > EOF
  12. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 12:31:54 2018
  13. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  14. Connected to:
  15. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  16. SQL> SQL> Name    Null?  Type
  17.  ----------------------------------------- -------- ----------------------------
  18.  STUID    NOT NULL NUMBER(4)
  19.  STUNAME     VARCHAR2(50)
  20. SQL>
  21. 1 row deleted.
  22. SQL>
  23. no rows selected
  24. SQL>
  25. 1 row created.
  26. SQL> insert into student values (1001, 'liumiao')
  27. *
  28. ERROR at line 1:
  29. ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
  30. SQL>
  31. 1 row created.
  32. SQL>
  33.    STUID STUNAME
  34. ---------- --------------------------------------------------
  35.    1001 liumiaocn
  36.    1003 michael
  37. SQL>
  38. Commit complete.
  39. SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  40. #

Mysql中類似的機制

mysql中使用source是否提供相關的類似機制的問題中,最終引入了Oracle此項功能在mysql中引入的建議,詳細請參看:

https://bugs.mysql.com/bug.php?id=73177

所以目前這只是一個sqlplus端的強化功能,並非標準,不同資料庫需要確認相應的功能是否存在。

小結

Oracle中使用WHENEVER SQLERROR進行出錯控制是否繼續,本文給出的例子非常簡單,詳細功能的使用可根據文中列出的Usage進行自行驗證和探索。

總結

以上就是Oracle資料庫多條sql執行語句出現錯誤時的控制方式的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值。

相關文章