這篇文章給大家分享的是有關(guān)Oracle基礎(chǔ)多條sql執(zhí)行在中間的語句出現(xiàn)錯誤時怎么辦的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)延安,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
環(huán)境準(zhǔn)備
使用Oracle的精簡版創(chuàng)建docker方式的demo環(huán)境
多行語句的正常執(zhí)行
對上篇文章創(chuàng)建的兩個字段的學(xué)生信息表,正常添加三條數(shù)據(jù),詳細(xì)如下:
# sqlplus system/liumiao123@XE <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語句,如果中間的一行出錯,缺省的狀況下第三行會不會被插入進(jìn)去?
我們將第二條insert語句的主鍵故意設(shè)定重復(fù),然后進(jìn)行確認(rèn)第三條數(shù)據(jù)是否會進(jìn)行插入即可。
# sqlplus system/liumiao123@XE <> > > > > 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 #
結(jié)果非常清晰地表明是會繼續(xù)執(zhí)行的,在oracle中通過什么來對其進(jìn)行控制呢?
WHENEVER SQLERROR
答案很簡單,在oracle中通過WHENEVER SQLERROR來進(jìn)行控制。
WHENEVER SQLERROR {EXIT [SUCCESS | FAILURE | WARNING | n | variable | :BindVariable] [COMMIT | ROLLBACK] | CONTINUE [COMMIT | ROLLBACK | NONE]}
WHENEVER SQLERROR EXIT
添加此行設(shè)定,即會在失敗的時候立即推出,接下來我們進(jìn)行確認(rèn):
# sqlplus system/liumiao123@XE <> > > > > > > > > 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則和缺省方式下的行為一致,出錯仍然繼續(xù)執(zhí)行
# sqlplus system/liumiao123@XE <> > > > > > > > > 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中類似的機(jī)制
mysql中使用source是否提供相關(guān)的類似機(jī)制的問題中,最終引入了Oracle此項功能在mysql中引入的建議,詳細(xì)請參看:
https://bugs.mysql.com/bug.php?id=73177
所以目前這只是一個sqlplus端的強(qiáng)化功能,并非標(biāo)準(zhǔn),不同數(shù)據(jù)庫需要確認(rèn)相應(yīng)的功能是否存在。
感謝各位的閱讀!關(guān)于“Oracle基礎(chǔ)多條sql執(zhí)行在中間的語句出現(xiàn)錯誤時怎么辦”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!