多条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. #

结果非常清楚地表明是会中男械模趏racle中颠末过程什么来对其停止节制呢?

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履行语句出现错误时的节制办法的全体内容了,盼望本文的内容对大家的学习或许工作具有一定的参考学习价值。