Oracle基础:通过sqlplus执⾏sql语句后的结果进⾏判断这篇⽂章介绍⼀下如何对sqlplus执⾏的sql语句结果进⾏判断。
环境准备
使⽤Oracle的精简版创建docker⽅式的demo环境,详细可参看:
常见问题
在sqlplus中执⾏sql语句,如果直接使⽤命令⾏的⽅式调⽤时会碰到两个问题:
问题1: 需要进⾏交互性的输⼊
问题2:结果的判断不能通过返回值来确认
解决⽅式
在脚本调⽤⾥,解决⽅式如下
问题1可以通过前⽂提到的Here Document来解决。
问题2,则可以通过对输出的判断来进⾏,因为Oracle的错误代码基本以ORA-打头,所以根据输出中是否包含ORA-,基本可确认是否存在错误。
执⾏结果判断⽰例
这⾥使⽤命令⾏的⽅式进⾏验证,直接拷贝到脚本中即可以使⽤脚本的⽅式与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;
> EOF
SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:18:51 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>
3 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>
Commit complete.
SQL>
STUID STUNAME
---------- --------------------------------------------------
1001 liumiaocn
1003 michael
SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
oracle@e871d42341c0:~$ echo $?
oracle@e871d42341c0:~$
从上⽂可以看到,三⾏insert的sql语句由于第⼆⾏的主键重复,出现错误,但是最终的结果使⽤命令⾏的⽅式⽆法对结果进⾏判断,这是控制台⽅式的常见场景,⽐如sftp或者ftp等也有此特点,⼀般常⽤的对应⽅式⽆法通过返回值进⾏判断,只能通过
输出来进⾏判断。
输出信息
输出分为标准输出和标准错误两种,输⼊输出的FD分别为:
标准输⼊:0
标准输出:1
标准错误:2
接下来我们看⼀下上⽂中的信息那些是标准输出,哪些是标准错误:
oracle@e871d42341c0:~$ sqlplus system/abcd1234@XE <<EOF 2& 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;
> EOF
oracle@e871d42341c0:~$
oracle@e871d42341c0:~$
oracle@e871d42341c0:~$
oracle@e871d42341c0:~$ cat output.info
SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:24:44 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:
基本的sql语句有哪些ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
SQL>
1 row created.
SQL>
Commit complete.
SQL>
STUID STUNAME
---------- --------------------------------------------------
1001 liumiaocn
1003 michael
SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
oracle@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;
> EOF
oracle@e871d42341c0:~$
oracle@e871d42341c0:~$ cat output.info
SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:29:31 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>
Commit complete.
SQL>
STUID STUNAME
---------- --------------------------------------------------
1001 liumiaocn
1003 michael
SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
oracle@e871d42341c0:~$
结果判断
使⽤grep确认是否存在ORA-相关的信息即可
oracle@e871d42341c0:~$ grep ORA- output.info
ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
oracle@e871d42341c0:~$ echo $?
oracle@e871d42341c0:~$
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,谢谢⼤家对的⽀持。如果你想了解更多相关内容请查看下⾯相关链接
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论