Oracle基础多条sql执行在中间的语句出现错误时的
多条sql执行时如果在中间的语句出现错误,后续会不会直接执行,如何进行设定,以及其他数据库诸如Mysql是如何对应的,这篇文章将会进行简单的整理和说明。
环境准备
使用Oracle的精简版创建docker方式的demo环境,详细可参看
多行语句的正常执行
对上篇文章创建的两个字段的学生信息表,正常添加三条数据,详细如下
# sqlplus system/liumiao123@XE <<EOF > desc student > select from student; > insert into student values (1001, 'liumiao'); > insert into student values (1002, 'liumiao'); > insert into student values (1003, 'michael'); > mit; > select from student; > EOF SQLPlus: Release 11.2.0.2.0 Production on Sun Oct 21 12:08:35 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> Name Null? Type ----------------------------------------- -------- ---------------------------- STUID NOT NULL NUMBER(4) STUNAME VARCHAR2(50) SQL> no rows selected SQL> 1 row created. SQL> 1 row created. SQL> 1 row created. SQL> Commit plete. SQL> STUID STUNAME ---------- -------------------------------------------------- 1001 liumiao 1002 liumiao 1003 michael SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production #
多行语句中间出错时的缺省动作
问题
三行insert语句,如果中间的一行出错,缺省的状况下第三行会不会被插入进去?
我们将第二条insert语句的主键故意设定重复,然后进行确认第三条数据是否会进行插入即可。
# sqlplus system/liumiao123@XE <<EOF desc student delete from student; select from student; insert into student values (1001, 'liumiao'); insert into student values (1001, 'liumiao'); insert into student values (1003, 'michael'); select from student; mit;> > > > > > EOF SQLPlus: Release 11.2.0.2.0 Production on Sun Oct 21 12:15:16 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> 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 liumiao 1003 michael SQL> SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production #
结果非常清晰地表明是会继续执行的,在oracle中通过什么来对其进行控制呢?
WHENEVER SQLERROR
答案很简单,在oracle中通过WHENEVER SQLERROR来进行控制。
WHENEVER SQLERROR {EXIT [SUCCESS | FAILURE | WARNING | n | variable | :BindVariable] [COMMIT | ROLLBACK] | CONTINUE [COMMIT | ROLLBACK | NONE]}
WHENEVER SQLERROR EXIT
添加此行设定,即会在失败的时候立即推出,接下来我们进行确认
# sqlplus system/liumiao123@XE <<EOF WHENEVER SQLERROR EXIT desc student delete from student; select from student; insert into student values (1001, 'liumiao'); insert into student values (1001, 'liumiao'); insert into student values (1003, 'michael'); select from student; mit;> > > > > > > > > > EOF SQLPlus: Release 11.2.0.2.0 Production on Sun Oct 21 12:27:15 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 Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production #
WHENEVER SQLERROR CONTINUE
使用CONTINUE则和缺省方式下的行为一致,出错仍然继续执行
# sqlplus system/liumiao123@XE <<EOF WHENEVER SQLERROR CONTINUE desc student delete from student; select from student; insert into student values (1001, 'liumiao'); insert into student values (1001, 'liumiao'); insert into student values (1003, 'michael'); select from student; mit;> > > > > > > > > > EOF SQLPlus: Release 11.2.0.2.0 Production on Sun Oct 21 12:31:54 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> 1 row 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 liumiao 1003 michael SQL> Commit plete. SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production #
Mysql中类似的机制
mysql中使用source是否提供相关的类似机制的问题中,最终引入了Oracle此项功能在mysql中引入的建议,详细请参看
所以目前这只是一个sqlplus端的强化功能,并非标准,不同数据库需要确认相应的功能是否存在。
小结
Oracle中使用WHENEVER SQLERROR进行出错控制是否继续,本文给出的例子非常简单,详细功能的使用可根据文中列出的Usage进行自行验证和探索。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对狼蚁SEO的支持。如果你想了解更多相关内容请查看狼蚁网站SEO优化相关链接
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程