這篇文章介紹一下如何對(duì)sqlplus執(zhí)行的sql語句結(jié)果進(jìn)行判斷。
環(huán)境準(zhǔn)備
使用Oracle的精簡(jiǎn)版創(chuàng)建docker方式的demo環(huán)境
常見問題
在sqlplus中執(zhí)行sql語句,如果直接使用命令行的方式調(diào)用時(shí)會(huì)碰到兩個(gè)問題:
解決方式
在腳本調(diào)用里,解決方式如下
執(zhí)行結(jié)果判斷示例
這里使用命令行的方式進(jìn)行驗(yàn)證,直接拷貝到腳本中即可以使用腳本的方式與sqlplus進(jìn)行集成。
oracle@e871d42341c0:~$ 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');> commit;> select * from student;> EOFSQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:18:51 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> Name Null? Type ----------------------------------------- -------- ---------------------------- STUID NOT NULL NUMBER(4) STUNAME VARCHAR2(50)SQL> 3 rows deleted.SQL> no rows selectedSQL> 1 row created.SQL> insert into student values (1001, 'liumiao')*ERROR at line 1:ORA-00001: unique constraint (SYSTEM.SYS_C007024) violatedSQL> 1 row created.SQL> Commit complete.SQL> STUID STUNAME---------- -------------------------------------------------- 1001 liumiaocn 1003 michaelSQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$ echo $?0oracle@e871d42341c0:~$
從上文可以看到,三行insert的sql語句由于第二行的主鍵重復(fù),出現(xiàn)錯(cuò)誤,但是最終的結(jié)果使用命令行的方式無法對(duì)結(jié)果進(jìn)行判斷,這是控制臺(tái)方式的常見場(chǎng)景,比如sftp或者ftp等也有此特點(diǎn),一般常用的對(duì)應(yīng)方式無法通過返回值進(jìn)行判斷,只能通過輸出來進(jìn)行判斷。
輸出信息
輸出分為標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤兩種,輸入輸出的FD分別為:
接下來我們看一下上文中的信息那些是標(biāo)準(zhǔn)輸出,哪些是標(biāo)準(zhǔn)錯(cuò)誤:
oracle@e871d42341c0:~$ sqlplus system/abcd1234@XE <<EOF 2>output.error 1>output.info> 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');> commit;> select * from student;> EOForacle@e871d42341c0:~$ oracle@e871d42341c0:~$ cat output.errororacle@e871d42341c0:~$ oracle@e871d42341c0:~$ cat output.infoSQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:24:44 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> Name Null? Type ----------------------------------------- -------- ---------------------------- STUID NOT NULL NUMBER(4) STUNAME VARCHAR2(50)SQL> 2 rows deleted.SQL> no rows selectedSQL> 1 row created.SQL> insert into student values (1001, 'liumiao')*ERROR at line 1:ORA-00001: unique constraint (SYSTEM.SYS_C007024) violatedSQL> 1 row created.SQL> Commit complete.SQL> STUID STUNAME---------- -------------------------------------------------- 1001 liumiaocn 1003 michaelSQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$
可以看到錯(cuò)誤信息全在標(biāo)準(zhǔn)輸出中,標(biāo)準(zhǔn)錯(cuò)誤中沒有信息。
重定向標(biāo)準(zhǔn)輸出與錯(cuò)誤判斷
雖然上述信息中可以看到,標(biāo)準(zhǔn)錯(cuò)誤中沒有信息,這里給出的方案是對(duì)應(yīng)常見的控制臺(tái)方式的錯(cuò)誤控制,為了保證標(biāo)準(zhǔn)錯(cuò)誤的信息不被遺漏,需要將標(biāo)準(zhǔn)錯(cuò)誤和重定向到標(biāo)準(zhǔn)輸出中,在bshell中寫法如下:
>輸出文件名稱 2>&1
結(jié)合本文的例子,使用方式如下:
oracle@e871d42341c0:~$ sqlplus system/abcd1234@XE <<EOF >output.info 2>&1> 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');> commit;> select * from student;> EOForacle@e871d42341c0:~$ oracle@e871d42341c0:~$ cat output.infoSQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:29:31 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> Name Null? Type ----------------------------------------- -------- ---------------------------- STUID NOT NULL NUMBER(4) STUNAME VARCHAR2(50)SQL> 2 rows deleted.SQL> no rows selectedSQL> 1 row created.SQL> insert into student values (1001, 'liumiao')*ERROR at line 1:ORA-00001: unique constraint (SYSTEM.SYS_C007024) violatedSQL> 1 row created.SQL> Commit complete.SQL> STUID STUNAME---------- -------------------------------------------------- 1001 liumiaocn 1003 michaelSQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Productionoracle@e871d42341c0:~$
結(jié)果判斷
使用grep確認(rèn)是否存在ORA-相關(guān)的信息即可
oracle@e871d42341c0:~$ grep ORA- output.info
ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
oracle@e871d42341c0:~$ echo $?
0
oracle@e871d42341c0:~$
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)VeVb武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選