這篇文章介紹一下如何對sqlplus執行的sql語句結果進行判斷。
環境準備
使用Oracle的精簡版創建docker方式的demo環境
常見問題
在sqlplus中執行sql語句,如果直接使用命令行的方式調用時會碰到兩個問題:
解決方式
在腳本調用里,解決方式如下
執行結果判斷示例
這里使用命令行的方式進行驗證,直接拷貝到腳本中即可以使用腳本的方式與sqlplus進行集成。
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語句由于第二行的主鍵重復,出現錯誤,但是最終的結果使用命令行的方式無法對結果進行判斷,這是控制臺方式的常見場景,比如sftp或者ftp等也有此特點,一般常用的對應方式無法通過返回值進行判斷,只能通過輸出來進行判斷。
輸出信息
輸出分為標準輸出和標準錯誤兩種,輸入輸出的FD分別為:
接下來我們看一下上文中的信息那些是標準輸出,哪些是標準錯誤:
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:~$
可以看到錯誤信息全在標準輸出中,標準錯誤中沒有信息。
重定向標準輸出與錯誤判斷
雖然上述信息中可以看到,標準錯誤中沒有信息,這里給出的方案是對應常見的控制臺方式的錯誤控制,為了保證標準錯誤的信息不被遺漏,需要將標準錯誤和重定向到標準輸出中,在bshell中寫法如下:
>輸出文件名稱 2>&1
結合本文的例子,使用方式如下:
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:~$
結果判斷
使用grep確認是否存在ORA-相關的信息即可
oracle@e871d42341c0:~$ grep ORA- output.info
ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
oracle@e871d42341c0:~$ echo $?
0
oracle@e871d42341c0:~$
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網的支持。
|
新聞熱點
疑難解答