多条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执行语句出现错误时的控制方式的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值。