您好,登录后才能下订单哦!
这篇文章介绍一下如何对sqlplus执行的sql语句结果进行判断。
环境准备
使用Oracle的精简版创建docker方式的demo环境,详细可参看:
https://www.jb51.net/article/153533.htm
常见问题
在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; > 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 $? 0 oracle@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; > EOF oracle@e871d42341c0:~$ oracle@e871d42341c0:~$ cat output.error 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: 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 $?
0
oracle@e871d42341c0:~$
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对亿速云的支持。如果你想了解更多相关内容请查看下面相关链接
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。