這篇文章給大家分享的是有關Oracle基礎多條sql執(zhí)行在中間的語句出現(xiàn)錯誤時怎么辦的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)致力于互聯(lián)網網站建設與網站營銷,提供做網站、成都網站制作、網站開發(fā)、seo優(yōu)化、網站排名、互聯(lián)網營銷、微信小程序開發(fā)、公眾號商城、等建站開發(fā),創(chuàng)新互聯(lián)網站建設策劃專家,為不同類型的客戶提供良好的互聯(lián)網應用定制解決方案,幫助客戶在新的全球化互聯(lián)網環(huán)境中保持優(yōu)勢。環(huán)境準備
使用Oracle的精簡版創(chuàng)建docker方式的demo環(huán)境
多行語句的正常執(zhí)行
對上篇文章創(chuàng)建的兩個字段的學生信息表,正常添加三條數(shù)據(jù),詳細如下:
# 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語句,如果中間的一行出錯,缺省的狀況下第三行會不會被插入進去?
我們將第二條insert語句的主鍵故意設定重復,然后進行確認第三條數(shù)據(jù)是否會進行插入即可。
# 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 #
結果非常清晰地表明是會繼續(xù)執(zhí)行的,在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 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中類似的機制
mysql中使用source是否提供相關的類似機制的問題中,最終引入了Oracle此項功能在mysql中引入的建議,詳細請參看:
https://bugs.mysql.com/bug.php?id=73177
所以目前這只是一個sqlplus端的強化功能,并非標準,不同數(shù)據(jù)庫需要確認相應的功能是否存在。
感謝各位的閱讀!關于“Oracle基礎多條sql執(zhí)行在中間的語句出現(xiàn)錯誤時怎么辦”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!