欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle基础多条sql执行在中间的语句出现错误时的控制方式

程序员文章站 2022-09-04 17:34:17
多条sql执行时如果在中间的语句出现错误,后续会不会直接执行,如何进行设定,以及其他数据库诸如mysql是如何对应的,这篇文章将会进行简单的整理和说明。 环境准备 使用...

多条sql执行时如果在中间的语句出现错误,后续会不会直接执行,如何进行设定,以及其他数据库诸如mysql是如何对应的,这篇文章将会进行简单的整理和说明。

环境准备

使用oracle的精简版创建docker方式的demo环境,详细可参看:

多行语句的正常执行

对上篇文章创建的两个字段的学生信息表,正常添加三条数据,详细如下:

# sqlplus system/liumiao123@xe <<eof
> desc student
> select * from student;
> insert into student values (1001, 'liumiaocn');
> insert into student values (1002, 'liumiao');
> insert into student values (1003, 'michael');
> commit;
> select * from student;
> eof
sql*plus: 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 complete.
sql> 
   stuid stuname
---------- --------------------------------------------------
   1001 liumiaocn
   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, 'liumiaocn');
insert into student values (1001, 'liumiao');
insert into student values (1003, 'michael');
select * from student;
commit;> > > > >    
> eof
sql*plus: 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 liumiaocn
   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, 'liumiaocn');
insert into student values (1001, 'liumiao');
insert into student values (1003, 'michael');
select * from student;
commit;> > > > > > > > > 
> eof
sql*plus: 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, 'liumiaocn');
insert into student values (1001, 'liumiao');
insert into student values (1003, 'michael');
select * from student;
commit;> > > > > > > > > 
> eof
sql*plus: 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 liumiaocn
   1003 michael
sql> 
commit complete.
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进行自行验证和探索。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接