Oracle基础多条sql执行在中间的语句出现错误时怎么办
这篇文章给大家分享的是有关Oracle基础多条sql执行在中间的语句出现错误时怎么办的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
环境准备
使用Oracle的精简版创建docker方式的demo环境
多行语句的正常执行
对上篇文章创建的两个字段的学生信息表,正常添加三条数据,详细如下:
#sqlplussystem/liumiao123@XE<<EOF>descstudent>select*fromstudent;>insertintostudentvalues(1001,'liumiaocn');>insertintostudentvalues(1002,'liumiao');>insertintostudentvalues(1003,'michael');>commit;>select*fromstudent;>EOFSQL*Plus:Release11.2.0.2.0ProductiononSunOct2112:08:352018Copyright(c)1982,2011,Oracle.Allrightsreserved.Connectedto:OracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProductionSQL>NameNull?Type-----------------------------------------------------------------------------STUIDNOTNULLNUMBER(4)STUNAMEVARCHAR2(50)SQL>norowsselectedSQL>1rowcreated.SQL>1rowcreated.SQL>1rowcreated.SQL>Commitcomplete.SQL>STUIDSTUNAME------------------------------------------------------------1001liumiaocn1002liumiao1003michaelSQL>DisconnectedfromOracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction#
多行语句中间出错时的缺省动作
问题:
三行insert语句,如果中间的一行出错,缺省的状况下第三行会不会被插入进去?
我们将第二条insert语句的主键故意设定重复,然后进行确认第三条数据是否会进行插入即可。
#sqlplussystem/liumiao123@XE<<EOFdescstudentdeletefromstudent;select*fromstudent;insertintostudentvalues(1001,'liumiaocn');insertintostudentvalues(1001,'liumiao');insertintostudentvalues(1003,'michael');select*fromstudent;commit;>>>>>>EOFSQL*Plus:Release11.2.0.2.0ProductiononSunOct2112:15:162018Copyright(c)1982,2011,Oracle.Allrightsreserved.Connectedto:OracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProductionSQL>NameNull?Type-----------------------------------------------------------------------------STUIDNOTNULLNUMBER(4)STUNAMEVARCHAR2(50)SQL>2rowsdeleted.SQL>norowsselectedSQL>1rowcreated.SQL>insertintostudentvalues(1001,'liumiao')*ERRORatline1:ORA-00001:uniqueconstraint(SYSTEM.SYS_C007024)violatedSQL>1rowcreated.SQL>STUIDSTUNAME------------------------------------------------------------1001liumiaocn1003michaelSQL>SQL>DisconnectedfromOracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction#
结果非常清晰地表明是会继续执行的,在oracle中通过什么来对其进行控制呢?
WHENEVER SQLERROR
答案很简单,在oracle中通过WHENEVER SQLERROR来进行控制。
WHENEVER SQLERROR {EXIT [SUCCESS | FAILURE | WARNING | n | variable | :BindVariable] [COMMIT | ROLLBACK] | CONTINUE [COMMIT | ROLLBACK | NONE]}
WHENEVER SQLERROR EXIT
添加此行设定,即会在失败的时候立即推出,接下来我们进行确认:
#sqlplussystem/liumiao123@XE<<EOFWHENEVERSQLERROREXITdescstudentdeletefromstudent;select*fromstudent;insertintostudentvalues(1001,'liumiaocn');insertintostudentvalues(1001,'liumiao');insertintostudentvalues(1003,'michael');select*fromstudent;commit;>>>>>>>>>>EOFSQL*Plus:Release11.2.0.2.0ProductiononSunOct2112:27:152018Copyright(c)1982,2011,Oracle.Allrightsreserved.Connectedto:OracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProductionSQL>SQL>NameNull?Type-----------------------------------------------------------------------------STUIDNOTNULLNUMBER(4)STUNAMEVARCHAR2(50)SQL>2rowsdeleted.SQL>norowsselectedSQL>1rowcreated.SQL>insertintostudentvalues(1001,'liumiao')*ERRORatline1:ORA-00001:uniqueconstraint(SYSTEM.SYS_C007024)violatedDisconnectedfromOracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction#
WHENEVER SQLERROR CONTINUE
使用CONTINUE则和缺省方式下的行为一致,出错仍然继续执行
#sqlplussystem/liumiao123@XE<<EOFWHENEVERSQLERRORCONTINUEdescstudentdeletefromstudent;select*fromstudent;insertintostudentvalues(1001,'liumiaocn');insertintostudentvalues(1001,'liumiao');insertintostudentvalues(1003,'michael');select*fromstudent;commit;>>>>>>>>>>EOFSQL*Plus:Release11.2.0.2.0ProductiononSunOct2112:31:542018Copyright(c)1982,2011,Oracle.Allrightsreserved.Connectedto:OracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProductionSQL>SQL>NameNull?Type-----------------------------------------------------------------------------STUIDNOTNULLNUMBER(4)STUNAMEVARCHAR2(50)SQL>1rowdeleted.SQL>norowsselectedSQL>1rowcreated.SQL>insertintostudentvalues(1001,'liumiao')*ERRORatline1:ORA-00001:uniqueconstraint(SYSTEM.SYS_C007024)violatedSQL>1rowcreated.SQL>STUIDSTUNAME------------------------------------------------------------1001liumiaocn1003michaelSQL>Commitcomplete.SQL>DisconnectedfromOracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction#
Mysql中类似的机制
mysql中使用source是否提供相关的类似机制的问题中,最终引入了Oracle此项功能在mysql中引入的建议,详细请参看:
https://bugs.mysql.com/bug.php?id=73177
所以目前这只是一个sqlplus端的强化功能,并非标准,不同数据库需要确认相应的功能是否存在。
感谢各位的阅读!关于“Oracle基础多条sql执行在中间的语句出现错误时怎么办”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。